我正在尝试在 Microsoft SQL Server 上运行这组 SQL 命令,但出现此错误:
Msg 137, Level 15, State 1, Line 1
必须声明标量变量“@dbstatus”。
我以为我确实声明了变量,所以我不确定为什么它仍然抛出错误?
DECLARE @dbname nvarchar(100)
DECLARE @dbstatus varchar(500)
DECLARE @sqlCommand NVARCHAR(1000)
create table #temptable (dbname nvarchar(100), status varchar(500))
DECLARE c1 CURSOR READ_ONLY
FOR
SELECT '[' + name + ']' FROM sys.databases WHERE name = 'EDDS1084543'
OPEN c1
FETCH NEXT FROM c1 INTO @dbname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlCommand = 'SET @dbstatus = (SELECT Status FROM ' + @dbname + '.[EDDSDBO].[dtSearchIndex])'
EXECUTE sp_executesql @sqlCommand
INSERT INTO #tempTable(dbname, [status])VALUES (@dbname, @dbstatus)
FETCH NEXT FROM c1 INTO @dbname
END
CLOSE c1
DEALLOCATE c1