【发布时间】:2017-02-26 09:01:18
【问题描述】:
我在不同的服务器上有多个数据库,我想从一个表中获取一些信息(例如用户-这个表在所有数据库上)。所以我在游标上添加所有服务器和数据库,然后运行这个脚本:
BEGIN
fetch next from CursorData into @DBName,@DBServer
set @cmd=':Connect ' + @DBServer+ CHAR(10) + CHAR(13)+ 'use ['+@DBName+'] select * from [UserAccount] where userid='''+@UserID+'''' + CHAR(10) + CHAR(13)+ 'GO'
print @cmd
exec sp_executeSQL @cmd
END
我有以下错误:
(1 row(s) affected)
:Connect DBServer
use [DBName] select * from [UserAccount] where userid='myuserid'
GO
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ':'.
Msg 911, Level 16, State 1, Line 2
Database 'DBName' does not exist. Make sure that the name is entered correctly.
为了在服务器之间切换,我使用了 sqlcmd 模式,但我不知道当我将它放入一个字符串然后执行字符串时是否有效。
如果,我在sqlcmd模式下执行这个命令工作:
:Connect DBServer
use [DBName] select * from [UserAccount] where userid='myuserid'
GO
如果有人知道我错过了什么或知道其他解决方案。
【问题讨论】:
标签: sql-server tsql ssms sqlcmd