2

I want to get the output by returning in the course question against the values from the following SQL query question. I want to record this print. How can I do that? Thank you for your help.

SQL query:

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

Cursor query:

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
  • 4
    you dont need a cursor for this. Check Ruslan's answer Commented Aug 28, 2017 at 12:38

1 Answer 1

5

Just do like this:

INSERT INTO etl.ErrorTable
   SELECT TABLE_SCHEMA + '.' + TABLE_NAME
   FROM INFORMATION_SCHEMA.TABLES 
   WHERE TABLE_SCHEMA = 'err';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.