Explain About Cursor in sql server

I reffered Many sites.I didn't get a clear explanation in cursor concepts.Please explain any one well known about cursor in sql server


9 Answers
1-9 of  9
9 Answers
  • we can use cursor when we need to update records in database. T-SQL used at one time for all the rows.it retrive data from all the rows

  • cursor copy the data in table row by row in  a specific condition
     

  • Cursor is concerned as a type of loop in sql server. Suppose you have to fetch one value from table1 and at the same time if you wanna execute any command to update some thing in other table i.e table2 by using the retrieved value from table1.In these type of scenario you can use the cursor.

  • Cursor is the private workspace which a user nedded Frst it has to be delcraed then it must be opened to use it nd then terminate it.

  • Cursor is the private workspace which a user nedded Frst it has to be delcraed then it must be opened to use it nd then terminate it.

  • As well as cursor is concern it is a type of loop in sql server.Let suppose you have to fetch one value from table1 and  at the same time if you wanna execute any command to update some thing in other table i.e table2 by using the retrieved value from table1.In these type of scenario you can use cursor

    Example Of cursor

    Declare @empcode varchar(50)
    Declare @Mycursor cursor
    SET @Mycursor =CURSOR FAST_FORWARD //---To speed up the cursor
    FOR
    select employeecode from tbl_employee_approvers_hierarchy where approverpriority=2 and hr=0
    OPEN @Mycursor 
    FETCH NEXT FROM @Mycursor into @empcode
    WHILE @@FETCH_STATUS=0
    BEGIN
    update tbl_employee_approvers_hierarchy  set approverpriority=3 where employeecode=@empcode and approvercode='NH-0172' and hr=1
    FETCH NEXT FROM @Mycursor into @empcode
    END
    CLOSE @Mycursor 
    DEALLOCATE @Mycursor 

    --Cursor may be fast_forward or forward_only but fast_forward is faster then forward_only--

    If there is still some confusion plz do let me know 





     

  • i need job with .net profile

Sql Server

Didn't get the answer.
Contact people of Talent-Sql Server directly by clicking here