第一步:聲明遊標
關鍵字(declare) 遊標名 關鍵字(cursor) 關鍵字(for)
記錄集
例如: declare cur_Name Cursor for Select * from TT
第二步
打開遊標 : open cur_Name
第四步
提取數據:Fetch Next From cur_Name [ into 變量List ]
第五步
循環提取:Fetch Next From cur_Name
while @@Fetch_Status=0
begin
Fetch Next From cur_Name
end
第六步:關閉釋放遊標
Close Cur_Name
deallocate cur_Name
例子 :
declare @faSysid int,
@mf varchar(400),
@nv varchar(400)
declare cur_AA cursor for select m_SysID,mdyfield,nvalue from T_AstModify where EntryID='DB0605010'
open cur_AA
Fetch Next From Cur_AA
into @faSysid,@mf,@nv
while @@fetch_Status=0
begin
select @faSysid,@mf,@nv
Fetch Next From Cur_AA
into @faSysid,@mf,@nv
end
close cur_AA
deallocate cur_aa