Cursors in Stored proc

  • hi,

    i have a stored proc that have seies of inner joins.now im planning to apply a cursor .how an i do that?

     

    thanks!

  • You must provide more information which will include

    1. Create table tblSample ([ID] INT Not NULL, Var1 Varchar(50) Not Null, etc.....)
    2. Insert Into tblSample SELECT 1,'test1',...
    3. Your requirements including why you want to use cursor etc...
    4. And read BOL for Cursor

    For an example of cursor, see below:

       DECLARE titles_cursor CURSOR FOR

       SELECT t.title

       FROM titleauthor ta, titles t

       WHERE ta.title_id = t.title_id AND

       ta.au_id = @au_id   -- Variable value from the outer cursor

       OPEN titles_cursor

       FETCH NEXT FROM titles_cursor INTO @title

       WHILE @@FETCH_STATUS = 0

       BEGIN

         

          SELECT @message = "         " + @title

          PRINT @message

          FETCH NEXT FROM titles_cursor INTO @title

      

       END

       CLOSE titles_cursor

       DEALLOCATE titles_cursor

  • Why use a cursor at all??

     

    What do you need to do exactly?

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply