如何在游标查询中使用我的SQL查询?

时间:2017-08-28 12:30:19

标签: sql-server

我希望通过返回针对以下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

1 个答案:

答案 0 :(得分:5)

就这样做:

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