我有一个游标及其获取数据库名称和链接服务器名称,以便在每个游标中执行一个过程
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 '.'
我该如何避免它?
答案 0 :(得分:1)
使用动态sql。像这样:
declare @sql nvarchar(max)
set @sql = 'exec ' + @LinkServerName + '.' + @DatabaseName + '.dbo.'+@someProcedure+' ''some'''
exec sp_executeSQL @sql