SQLServer游标(Cursor)简单例子

Posted on 2021-12-01 10:24  东方姑娘  阅读(340)  评论(0编辑  收藏  举报
DECLARE @username nvarchar(50),@password nvarchar(50),@num int--声明游标变量
DECLARE myCursor CURSOR FOR select username,password,num from dt_admin WHERE types=1 --创建游标
OPEN myCursor --打开游标
FETCH NEXT FROM myCursor INTO @username,@password,@num --给游标变量赋值
--开始循环
WHILE @@FETCH_STATUS=0 --判断FETCH语句是否执行执行成功
BEGIN
print 'INSERT INTO dt_admin (username,password,num)VALUES('''+@username+''','''+@password+''','+cast(@num as nvarchar(50))+')' --打印sql(对每一行数据进行操作)
FETCH NEXT FROM myCursor INTO @username,@password,@num  --下一个游标变量赋值
END
CLOSE myCursor --关闭游标
DEALLOCATE myCursor --释放游标

  

Copyright © 2024 东方姑娘
Powered by .NET 9.0 on Kubernetes