--切换数据库
use MyDatabase
--创建游标(scroll:滚动游标)
declare mycur cursor scroll for select EmpNo from Employee
--打开游标
open mycur
--关闭游标
close mycur
--删除游标
deallocate mycur
--提取第一行
--利用游标进行数据的修改
fetch absolute 2 from mycur
update Employee
set EmpPhone = '55-6666-7777' where current of mycur
--利用游标进行数据的删除
fetch last from mycur
delete from Employee where current of mycur
select * from Employee
--创建游标(指向多列)
declare columns cursor scroll for select EmpNo,EmpName,EmpPhone from Employee
--打开游标
open columns
--关闭游标
close columns
--删除游标
deallocate columns
--遍历游标
declare @empno int
declare @empname varchar(30)
declare @empphone varchar(30)
fetch absolute 1 from columns into @empno,@empname,@empphone
while @@FETCH_STATUS = 0
begin
print '社員No:' + cast( @empno as varchar(3)) + ' 社員名前:' + @empname + ' 社員電話番号:' + @empphone
fetch next from columns into @empno,@empname,@empphone
end
遍历游标结果: