我有一个跟随光标:
DECLARE MY_CURSOR Cursor
FOR
SELECT [DB_NAME],[SCHEMA_NAME],[TABLE_NAME],[COLUMN_NAME] FROM dbo.Data_Profile_Stats
Open My_Cursor
DECLARE @DB_NAME nvarchar(500),@SCHEMA_NAME nvarchar(500),@TABLE_NAME nvarchar(500),@COLUMN_NAME nvarchar(500), @QUERY NVARCHAR(MAX)
DECLARE @MAX_NUMBER NVARCHAR(255), @MIN_NUMBER INT, @NULL_COUNT INT, @BLANK_COUNT INT, @ZERO_COUNT INT,@DISTINCT_COUNT INT
Fetch NEXT FROM MY_CURSOR INTO @DB_NAME,@SCHEMA_NAME,@TABLE_NAME,@COLUMN_NAME
While (@@FETCH_STATUS = 0)
BEGIN
SELECT @DB_NAME,@SCHEMA_NAME,@TABLE_NAME,@COLUMN_NAME
SET @QUERY='SELECT MAX('+@DB_NAME+'.'+@SCHEMA_NAME+'.'+@TABLE_NAME+'.'+@COLUMN_NAME+') FROM '+@DB_NAME+'.'+@SCHEMA_NAME+'.'+@TABLE_NAME
--SET @MAX_NUMBER=exec sp_executesql @QUERY
EXEC SP_EXECUTESQL @QUERY, N'@MAX_NUMBER NVARCHAR(255) OUTPUT', @MAX_NUMBER=@MAX_NUMBER OUTPUT
SELECT @MAX_NUMBER
--SET @QUERY='UPDATE [dbo].[Data_Profile_Stats] SET MAX_NUMBER='+CAST(@MAX_NUMBER AS VARCHAR(10))+' WHERE [DB_NAME]='+''''+@DB_NAME+''''+' AND SCHEMA_NAME='+''''+@SCHEMA_NAME+''''+' AND TABLE_NAME='+''''+@TABLE_NAME+''''+' AND COLUMN_NAME='+''''+@COLUMN_NAME+''''
--exec sp_executesql @QUERY
FETCH NEXT FROM MY_CURSOR INTO @DB_NAME,@SCHEMA_NAME,@TABLE_NAME,@COLUMN_NAME
END
CLOSE MY_CURSOR
DEALLOCATE MY_CURSOR
GO
在上面的查询中,我看到EXEC SP_EXECUTESQL的结果,但无法在下一行中看到@MAX_NUMBER的值
有人能告诉我我做错了什么吗?
提前致谢
答案 0 :(得分:0)
您的查询未设置@MAX_NUMBER。试试这个:
SET @QUERY='SELECT @MAX_NUMBER=MAX('+@DB_NAME+'.'+@SCHEMA_NAME+'.'+@TABLE_NAME+'.'+@COLUMN_NAME+') FROM '+@DB_NAME+'.'+@SCHEMA_NAME+'.'+@TABLE_NAME;