Need help understanding some cursor code

I need help understanding some sql which uses Cursors. Look at lines 7 and 8 below. What is this code doing? Is it selecting the very first row of the cursor and storing it in the variables (@lchCountryCode, @lchPrefix, @lchSuffix), or is it selecting the last row? I need some help understanding this and am seeking expert help please.

2     FOR

9      INTO @lchCountryCode, @lchPrefix, @lchSuffix
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
It is basically pulling the first row of the dataset into the variables.  Typically you would have an OPEN then a FETCH to pull the first row then a while loop to cycle through the remaining rows with an identical FETCH as the last action within the loop to pull the subsequent row of data.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vikas GargAssociate Principal EngineerCommented:

Life Cycle of Cursor

    1. Declare Cursor

     A cursor is declared by defining the SQL statement that returns a result set.
   2. Open

    A Cursor is opened and populated by executing the SQL statement defined by the cursor.
3. Fetch

    When cursor is opened, rows can be fetched from the cursor one by one or in a block to do data manipulation.
4. Close

After data manipulation, we should close the cursor explicitly.

5.  Deallocate

    Finally, we need to delete the cursor definition and released all the system resources associated with the cursor.

Syntax to Declare Cursor

Declare Cursor SQL Comaand is used to define the cursor with many options that impact the scalablity and loading behaviour of the cursor. The basic syntax is given below

     DECLARE cursor_name CURSOR
     [LOCAL | GLOBAL] --define cursor scope
     [FORWARD_ONLY | SCROLL] --define cursor movements (forward/backward)
     [STATIC | KEYSET | DYNAMIC | FAST_FORWARD] --basic type of cursor
     [READ_ONLY | SCROLL_LOCKS | OPTIMISTIC] --define locks
     FOR select_statement --define SQL Select statement
     FOR UPDATE [col1,col2,...coln] --define columns that need to be updated 

Syntax to Open Cursor

A Cursor can be opened locally or globally. By default it is opened locally. The basic syntax to open cursor is given below:

     OPEN [GLOBAL] cursor_name --by default it is local 

Syntax to Fetch Cursor

Fetch statement provides the many options to retrieve the rows from the cursor. NEXT is the default option. The basic syntax to fetch cursor is given below:

    FROM [GLOBAL] cursor_name 
    INTO @Variable_name[1,2,..n] 

Syntax to Close Cursor

Close statement closed the cursor explicitly. The basic syntax to close cursor is given below:

     CLOSE cursor_name --after closing it can be reopen 

Syntax to Deallocate Cursor

Deallocate statement delete the cursor definition and free all the system resources associated with the cursor. The basic syntax to close cursor is given below:

     DEALLOCATE cursor_name --after deallocation it can't be reopen 

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.