Wednesday, May 22, 2013

How to Create Cursor

declare @tempt table
(
ID int null,
Name varchar(100) null
)
DECLARE @vendor_id int, @vendor_name nvarchar(50),
    @message varchar(80), @product nvarchar(50);
PRINT '-------- Vendor Products Report --------';
-- Declare Cursor – untill it deallocate
-- You Con’t declare another cursor with same name.
DECLARE vendor_cursor CURSOR FOR
SELECT ID,Ag_Fname
from AgentMaster
OPEN vendor_cursor
FETCH NEXT FROM vendor_cursor
INTO @vendor_id, @vendor_name
WHILE @@FETCH_STATUS = 0
BEGIN
   
    SELECT @message = '----- Vendor: ' +
        @vendor_name
    PRINT @message
    insert INTO @tempt values (@vendor_id, @vendor_name)
   
    FETCH NEXT FROM vendor_cursor
    INTO @vendor_id, @vendor_name
END
CLOSE vendor_cursor;
select * from @tempt
-- Deallocate Cursor
DEALLOCATE vendor_cursor;

5 comments:

  1. Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. Web Developer Gold Coast

    ReplyDelete
  2. Agile software development You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this...

    ReplyDelete
  3. Thank you because you have been willing to share information with us. Digital Agency

    ReplyDelete
  4. I admire this article. You have written a well-researched content and pick excellent wording. I got so much involved in this that I couldn’t stop reading. I am impressed with your skills and work. Thank you! Microsoft.net Development Services

    ReplyDelete
  5. Very useful info. Hope to see more posts soon!. Outsourcing development

    ReplyDelete