从MS SQL到MySQL的动态SQL查询

| 我有2个数据库,一个在MS SQL上,另一个在Mysql上。我写了一些sql scriots,它们从Mysql中获取数据并插入到MS SQL中。为此,我在MS SQL上设置了设备驱动程序连接和链接服务器,并且这些脚本在MS SQL上执行。脚本很简单,只有select语句。 现在,我需要编写动态脚本,以基于一些参数从mysql获取数据。下面是从DB \“ stagedb_ie \”中获取数据的示例查询,即代表爱尔兰。
select * from openquery(stagedb_ie, \'select * from stagedb_ie.aol_center\')
mysql中还有其他数据库,其国家代码后缀以stagedb作为名称。现在,我想将此国家/地区代码作为参数进行查询和获取数据。例如
declare @stagedb_country varchar(20)
set @stagedb_country = \'stagedb_ie\'
select * from openquery(@stagedb_country, \'select * from \'+ @stagedb_country +\'.aol_center\')
但是此查询不起作用。有什么建议吗?     
已邀请:
        
declare @stagedb_country varchar(20),
   @SQL nvarchar(max);
set @stagedb_country = \'stagedb_ie\';
set @SQL = \'select * from openquery(\' + @stagedb_country
   + \', \'\'select * from \' + @stagedb_country + \'.aol_center\'\')\';
exec (@SQL);
请注意,SP中的动态SQL是在调用者而非SP所有者的权限下执行的。 最后,我想知道为什么要为此使用单独的数据库和表。您是否可以对数据进行编码,而不是将数据放入不同的容器中?     

要回复问题请先登录注册