我正在生成动态脚本,如果表不存在,则将数据移动到另一个数据库中,然后我要创建。如果直接执行此脚本,则该脚本可以完美运行。但是如果该脚本字符串由execute语句执行则给出错误。我也试过执行。
declare @temp as varchar(max)
set @temp='select * into Allocation_Archive.dbo.Users from Users'
execute @temp
错误
Database 'select * into Allocation_Archive' does not exist. Make sure that the name is entered correctly.
USE database
在变量中添加一条语句,或指定完整的对象名称,包括数据库和架构。@temp
变量括在括号中例如:
declare @temp as varchar(max)
set @temp='select * into Allocation_Archive.dbo.Users from ThisDatabase.dbo.Users'
execute @temp
不要使用:
EXECUTE @temp
改为使用
EXEC sp_executesql @temp
您还需要将@temp
变量更改为nvarchar(max)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句