0

我有一个以下光标:

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 的值

有人可以告诉我我做错了什么吗?

提前致谢

4

1 回答 1

0

您的查询未设置 @MAX_NUMBER。尝试这个:

SET @QUERY='SELECT @MAX_NUMBER=MAX('+@DB_NAME+'.'+@SCHEMA_NAME+'.'+@TABLE_NAME+'.'+@COLUMN_NAME+') FROM '+@DB_NAME+'.'+@SCHEMA_NAME+'.'+@TABLE_NAME;
于 2013-04-19T17:19:19.597 回答