Use GPT to build an SQL cursor for you (saves a ton of time)

by | Jul 21, 2023 | ChatGPT | 0 comments

Designed for ChatGPT Version: gpt-4

Prompt Text

Please write me a cursor to iterate over this table with all necessary variables declared: [Table / TempTable structure]

 

Prompt Description

As an example:

Please write me a cursor to iterate over this table with all necessary variables declared:

CREATE TABLE #TempContactIDs
(
ContactID INT
);

This saves SO much time for menial, repetitive cursor creation.  That query will result in the following:

 

 


DECLARE @CurrentContactID INT;

DECLARE ContactCursor CURSOR FOR 

SELECT ContactID FROM #TempContactIDs;

OPEN ContactCursor;

FETCH NEXT FROM ContactCursor INTO @CurrentContactID;

WHILE @@FETCH_STATUS = 0

BEGIN

    -- Here is where you can use @CurrentContactID to do whatever you need.

    -- This example just prints it.

    PRINT @CurrentContactID;

    FETCH NEXT FROM ContactCursor INTO @CurrentContactID;

END;

CLOSE ContactCursor;

DEALLOCATE ContactCursor;

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *