How to Use Cursor

    xiaoxiao2021-12-14  18

    Below link is a good source for learning cursors in sql 

    http://www.cnblogs.com/moss_tan_jun/archive/2011/11/26/2263988.html 

     

    And I already made a little script for  example. 

    The function of this script is, to check  id & dcid for all students, 

    Then they 2 are not equal then change make this id to dcid's value. 

    And delete this cursor to free space in RAM. 

    Please make a cursor by yourself! 

     

    DECLARE 

    test_cursor CURSOR  

    FOR 

    select id,DCID FROM STUDENTS where dcid!=id 

    OPEN test_cursor 

    DECLARE @ID NVARCHAR(10) 

    DECLARE @DCID NVARCHAR(10) 

    FETCH NEXT FROM test_cursor INTO @ID,@DCID 

    while @@fetch_status=0 

    begin 

    print @id 

    print @dcid 

    update students set id=@dcid 

    where dcid=@dcid 

    fetch next from test_cursor into @id,@dcid 

    end 

    close test_cursor 

    deallocate test_cursor 

     

    Feel free to let me know if you have any question about this. 

     

    FYI, forum is a good source for learning everything about PC/MAC. 

    Here is a link, you can login with WeChat QR code. 

    http://www.csdn.net/

    转载请注明原文地址: https://ju.6miu.com/read-963351.html

    最新回复(0)