1

我有一个游标及其获取数据库名称和链接服务器名称,以在每个游标中执行一个过程

Declare getDBLinkServer cursor for  
select DatabaseName, LinkServerName from DBnLinkServer  
Open getDBLinkServer  
fetch next from getDBLinkServer into @DatabaseName, @LinkServerName  
While @@FETCH_STATUS = 0  
Begin  
    exec @LinkServerName.@DatabaseName.dbo.someProcedure 'some' 
    fetch next from getDBLinkServer into @DatabaseName, @LinkServerName  
End  
Close getDBLinkServer  
Deallocate getDBLinkServer

这显示了Incorrect syntax near '.'如何避免它?

4

1 回答 1

1

使用动态 sql。像这样:

declare @sql nvarchar(max)
set @sql = 'exec ' + @LinkServerName + '.' + @DatabaseName + '.dbo.'+@someProcedure+' ''some''' 

exec sp_executeSQL @sql
于 2012-12-04T10:33:29.477 回答