我正在运行循环,循环遍历一堆数据库,在几个命令中有一个安装程序。我遇到了这个问题。我尝试了一些方法,但是他们错了。
*'use '+@DBName+';
CREATE proc [dbo].proc_ContributorTapHistoryUpdate ....*
Msg 111,Level 15,State 1,Line 2' CREATE PROCEDURE'必须是 查询批处理中的第一个语句。
*CREATE proc '+@DBName+'.[dbo].proc_ContributorTapHistoryUpdate .....*
Msg 166,Level 15,State 1,Line 2' CREATE PROCEDURE'不允许 将数据库名称指定为对象名称的前缀。
代码示例:
declare @DBName varchar(20)
declare ProjectList cursor FORWARD_ONLY READ_ONLY for
select name as DBName
from sysdatabases
where isnumeric(substring(name,4,40)) = 1
open ProjectList
fetch next from ProjectList
into @DBName
while @@FETCH_STATUS = 0
begin
execute('use '+@DBName+' go
CREATE proc [dbo].proc_ContributorTapHistoryUpdate
as
begin
select * from sysobjects where name = ''FU''
end
')
if @@ERROR <> 0
goto ERR_EXIT
fetch next from ProjectList
into @DBName
end
ERR_EXIT:
close ProjectList
deallocate ProjectList