游标怎样使用(简单点的)
我最近要用的游标,但以前没有用过,不知道从何下手啊,请高手来帮帮忙哦
[解决办法]
SQL的游标?
see
DECLARE Employee_Cursor CURSOR FOR
SELECT LastName, FirstName
FROM Northwind.dbo.Employees
WHERE LastName like 'B% '
OPEN Employee_Cursor
FETCH NEXT FROM Employee_Cursor
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM Employee_Cursor
END
CLOSE Employee_Cursor
DEALLOCATE Employee_Cursor
[解决办法]
第一步:声明游标
关键字(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