我有 2 个数据库,一个在 MS SQL 上,另一个在 Mysql 上。我已经编写了一些从 Mysql 获取数据并插入 MS SQL 的 sql scriots。为此,我在 MS SQL 上设置了设备驱动程序连接和链接服务器,这些脚本在 MS SQL 上执行。脚本很简单,只有选择语句。现在我需要编写动态脚本来根据一些参数从 mysql 中获取数据。下面是从数据库“stagedb_ie”获取数据的示例查询,其中 ie 代表爱尔兰。
select * from openquery(stagedb_ie, 'select * from stagedb_ie.aol_center')
mysql中还有其他dbs,国家代码后缀为stagedb作为名称。现在我想通过这个国家代码作为参数来查询和获取数据。例如
declare @stagedb_country varchar(20)
set @stagedb_country = 'stagedb_ie'
select * from openquery(@stagedb_country, 'select * from '+ @stagedb_country +'.aol_center')
但是这个查询不起作用。有什么建议吗?