Home purpose of Microsofts FETCH loop example?
Reply: 0

purpose of Microsofts FETCH loop example?

user1535 Published in June 19, 2018, 6:14 pm

I see this example in Microsofts documentation

USE AdventureWorks2012;  
DECLARE contact_cursor CURSOR FOR  
SELECT LastName FROM Person.Person  
WHERE LastName LIKE 'B%'  
ORDER BY LastName;  

OPEN contact_cursor;  

-- Perform the first fetch.  
FETCH NEXT FROM contact_cursor;  

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.  
   -- **This is executed as long as the previous fetch succeeds.**  
   FETCH NEXT FROM contact_cursor;  

CLOSE contact_cursor;  
DEALLOCATE contact_cursor;  

I'm wondering and asking

a) what happens inside the FETCH-WHILE loop? It seems to do just nothing, except skipping over the comment. Can one possibly do something on the data, without using 'INTO'?

b) how to access the column data from the current row, inside the loop, in T-SQL? Preferrably just contact_cursor.LastName .

Yes I read that I should work 'set-based', and cursors are not efficient. But I'm allowed to do processing (string manips, testing them, and in the end one UPDATE per row) in T-SQL and not in the outside procedural environment, and do it carefully in a robust way, and it is just a couple of records, once+testing. And me perfectly new to T-SQL, but old to SQL and many other things. T-SQL is 2016 when it matters.

Plz I can't reply as comments, because my level is too low. I'm grateful for help, for the explanation of this. Thank you!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306847 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO