游标
来源:互联网 发布:知君的书法 编辑:程序博客网 时间:2024/05/29 15:20
--- mssql if not cursor_status('global','c_cur')=-3 and not cursor_status('local','c_cur')=-3 -- 存在时候销毁游标 begin close c_cur; deallocate c_cur; end declare c_cur cursor for select id from XXX; declare @v_id nvarchar(50) begin open c_cur; fetch from c_cur into @v_id; while @@fetch_status=0 begin if cursor_status('global','c_cur')=-3 and cursor_status('local','c_cur')=-3 begin print '不存在' end else begin print '存在' end fetch from c_cur into @v_ID; loop: end; close c_cur; deallocate c_cur; end; -- oracle 当然可以使用简化的for循环 declare cursor emp_cur is select * from emp; empRecord emp%rowtype; begin open emp_cur; loop fetch emp_cur into empRecord; exit when emp_cur%notfound; dbms_output.put_line(empRecord.ename); end loop; close emp_cur; end; -- mysql declare v_d datetime; declare no_more_times integer default 0; -- 定义游标遍历时,作为判断是否遍历完全部记录的标记 declare c_cur cursor for select now(); -- 定义游标 declare continue handler for not found set no_more_times=1; -- 遍历完之后值 open c_cur; repeat fetch c_cur into v_datetime; -- if no_more_times<>1 then loop; set @s=concat("select now(),'",v_datetime,"';") prepare s from @s; execute s; until no_more_times end repeat; -- 结束循环 close c_cur;
0 0