SQL server游标基本结构

时间:2021-09-18 04:21:03

简单游标遍历数据:

BEGIN 
    DECLARE QZ_cursor CURSOR SCROLL FOR /*创建游标*/
    SELECT NAME FROM USERINFO/*要遍历的数据*/
    OPEN QZ_cursor/*打开游标*/
    DECLARE @v_ID VARCHAR(100)/*创建临时变量*/
    FETCH NEXT FROM QZ_cursor INTO @v_ID
    WHILE @@FETCH_STATUS=0
    BEGIN/*进行循环遍历*/
		PRINT @v_ID
        FETCH NEXT FROM QZ_cursor INTO @v_ID
    END
    CLOSE QZ_cursor	/*关闭游标*/
    DEALLOCATE QZ_cursor  /*销毁游标*/
END