Articles → SQL SERVER → Cursors In SQL Server

Cursors In SQL Server







  1. Declare a cursor
  2. Declare cursor_name cursor for select_Statement




  3. Open a cursor
  4. Open cursor_name


  5. Fetch a value from the cursor
  6. fetch next from cursor_name [into variable_name]


  7. Fetch till the last record in the result set
  8. while @@fetchstatus = 0 
    begin
       // Do something fetch next 
       from
          cursor_name [into variable_name] 
    end


  9. Close a cursor


  10. close cursor_name


  11. Deallocate a cursor
deallocate cursor_name




PK_IDsName
1Name1
2Name2




Declare @con as varchar(50) 
Declare @temp as varchar(50) 
Set
   @con = '' 
   Set
      @temp = '' 
      Declare mycursor cursor for 
      select
         sName 
      from
         tbl_Test open mycursor fetch next 
      from
         mycursor into @con while @@fetch_status = 0 
         begin
      Set
         @con = @con + @temp fetch next 
      from
         mycursor into @temp 
         end
         close mycursor deallocate mycursor 
         select
            @con  





Posted By  -  Karan Gupta
 
Posted On  -  Friday, April 30, 2010

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250