SQL 游标使用细节
定义游标时用默认的申明时
1.如果用到了order by ,order by 里面没有主键的话,所在的修改不会更新映射到游标去。
2.没有order by 的时候则没事。
3.多表关联时,如果order by 没有用到主键。所在的修改不会更新映射到游标去。
IF OBJECT_ID('tempdb..##a') IS NOT NULL DROP TABLE ##a CREATE TABLE ##a(id INT IDENTITY(1,1) , mc VARCHAR(50)) INSERT INTO ##a(mc) SELECT '1' UNION ALL SELECT '2' DECLARE @b TABLE(mc int) INSERT INTO @b ( mc ) SELECT '1' UNION ALL SELECT '2' EXEC dbo.Create_CLUSTERED_IX @TableName = '##a', -- varchar(255) @FiledName = 'id', -- varchar(500) @IXName = 'ix_tmp_a_id' -- varchar(255) DECLARE @id INT,@mc VARCHAR(50) DECLARE cursor_name CURSOR DYNAMIC FOR SELECT a.id,a.mc FROM ##a a --WHERE a.id=b.mc ORDER BY mc OPEN cursor_name FETCH NEXT FROM cursor_name INTO @id,@mc While @@FETCH_STATUS<>-1 BEGIN SELECT @mc UPDATE ##a SET mc='33' FETCH NEXT FROM cursor_name INTO @id,@mc END CLOSE cursor_name DEALLOCATE cursor_name /* declare variables */