Get Multiple Values in SQL Server Cursor

后端 未结 2 770
傲寒
傲寒 2020-12-02 11:01

I have a cursor containing several columns from the row it brings back that I would like to process at once. I notice most of the examples I\'ve seeing on how to use cursor

2条回答
  •  余生分开走
    2020-12-02 11:34

    This should work:

    DECLARE db_cursor CURSOR FOR SELECT name, age, color FROM table; 
    DECLARE @myName VARCHAR(256);
    DECLARE @myAge INT;
    DECLARE @myFavoriteColor VARCHAR(40);
    OPEN db_cursor;
    FETCH NEXT FROM db_cursor INTO @myName, @myAge, @myFavoriteColor;
    WHILE @@FETCH_STATUS = 0  
    BEGIN  
    
           --Do stuff with scalar values
    
           FETCH NEXT FROM db_cursor INTO @myName, @myAge, @myFavoriteColor;
    END;
    CLOSE db_cursor;
    DEALLOCATE db_cursor;
    

提交回复
热议问题