游标的应用

--use sms
--goto
--定义游标
/*
declare 游标名称 cursor
[local | clobal]
[forward_only|scroll]
[static|keyset|dynamic|fast_forward]
[read_only|scroll_locks|optimistic]
[type_warning]
for select 语句
[for update[for 字段名][,...n]]]
根据帮助理解各项含义
*/
declare cj_cursor cursor
scroll
for
select zkzh 身份证号,cj33 笔试,cj44 上机,zcj 总成绩 from ncre26

--打开游标

open cj_cursor

--从游标中提取一行记录
--由于未指定scroll选项,fetch next是唯一可用的可提取选项
fetch next from cj_cursor
fetch next from cj_cursor
fetch next from cj_cursor
fetch next from cj_cursor
fetch next from cj_cursor
fetch prior from cj_cursor
fetch first from cj_cursor
fetch next from cj_cursor
fetch absolute 3 from cj_cursor
fetch relative -1 from cj_cursor
--察看有几行
--if @@cursor_rows>0
print '共有'+convert(varchar,@@cursor_rows)
--关闭游标
close cj_cursor
--删除游标
deallocate cj_cursor

你可能感兴趣的:(游标)