2017-08-28 93 views
2

我想通過在課程問題中返回以下SQL查詢問題的值來獲取輸出。我想記錄這張照片。我怎樣才能做到這一點?感謝您的幫助。如何在遊標查詢中使用我的SQL查詢?

SQL查詢:

SELECT 
    [NoMatchExplanation], 
    COUNT(*) 
FROM 
    err.CustomerBank 
GROUP BY 
    (NoMatchExplanation) 

光標查詢:

DECLARE cr_Read CURSOR FOR 
    SELECT TABLE_SCHEMA + '.' + TABLE_NAME 
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_SCHEMA = 'err' 

DECLARE @name NVARCHAR(100) 

OPEN cr_Read 

FETCH NEXT FROM cr_Read INTO @name 

WHILE @@FETCH_STATUS = 0 
BEGIN 
    INSERT INTO etl.ErrorTable VALUES (@name) 

    FETCH NEXT FROM cr_Read INTO @name 
END 

CLOSE cr_Read 
DEALLOCATE cr_Read 
+4

你不需要爲這個光標。檢查魯斯蘭的答案 – GuidoG

回答

5

就這樣做:

INSERT INTO etl.ErrorTable 
    SELECT TABLE_SCHEMA + '.' + TABLE_NAME 
    FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_SCHEMA = 'err';