数据库
首页 > 数据库> > SQLServer游标(Cursor)简单例子

SQLServer游标(Cursor)简单例子

作者:互联网

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 --释放游标

  

标签:username,myCursor,--,SQLServer,游标,Cursor,num,password
来源: https://www.cnblogs.com/xiangjiao/p/15627751.html