--声明2个变量
declare @rownum nvarchar(20)
declare @num int
set @num =0
--声明一个游标mycursor,select语句中参数的个数必须要和从游标取出的变量名相同
declare mycursor cursor for select row_number() over(order by Code) as rownum from T_RolePermisModule
--打开游标
open mycursor
--从游标里取出数据赋值到我们刚才声明的2个变量中
fetch next from mycursor into @rownum
--判断游标的状态
--0 fetch语句成功
---1 fetch语句失败或此行不在结果集中
---2被提取的行不存在
while (@@fetch_status=0)
begin
--显示出我们每次用游标取出的值
print '游标成功取出一条数据'
set @num =@num+1
print @num
--UPDATE T SET [iIndex] = @num
--05下有莫名奇妙的横线,不显示
UPDATE T SET [iIndex] =STUFF('TT000',6-LEN(@num),LEN(@num),@num)
from (select *,row_number() over(order by [iIndex]) as rownum from T_RolePermisModule) T
where @rownum =rownum
--用游标去取下一条记录
fetch next from mycursor into @rownum
end
--关闭游标
close mycursor
--撤销游标
deallocate mycursor