断点

每天进步一点点!
posts - 174, comments - 56, trackbacks - 0, articles - 21

cursor游标

Posted on 2010-07-24 16:14 断点 阅读(236) 评论(0)  编辑  收藏 所属分类: Oracle DBA

--游标
declare
  cursor c is
    select * from emp;
  v_emp c%rowtype;
begin
  open c;
  loop
    fetch c into v_emp;
    exit when(c%notfound);
    dbms_output.put_line(v_emp.ename);
  end loop;
  close c;
end;

declare
  cursor c is
    select * from emp;
  v_emp emp%rowtype;
begin
  open c;
  fetch c into v_emp;
    while(c%found) loop
      dbms_output.put_line(v_emp.ename);
      fetch c into v_emp;
      --fetch c into v_emp; 导致第一条没有打印,最后一条打印2遍。
      --dbms_output.put_line(v_emp.ename);
  end loop;
  close c;
end;


declare
  cursor c is
    select * from emp;
begin
  for v_emp in c loop
    dbms_output.put_line(v_emp.ename);
  end loop;
end;


--带参数的游标
declare
  cursor c(v_deptno emp.deptno%type,v_job emp.job%type)
  is
    select ename,sal from emp where deptno =v_deptno and job= v_job;
    --v_temp c%rowtype;
begin
  for v_temp in c(30,'CLERK') loop  --for自动打开游标。
    dbms_output.put_line(v_temp.ename);
  end loop;
end;


--可更新的游标
declare
  cursor c
  is
    select * from emp2 for update;
    --v_temp c%rowtype;
begin
  for v_temp in c loop 
    if(v_temp.sal <2000) then
      update emp2 set sal = sal*2 where current of c;
    elsif(v_temp.sal = 5000) then
      delete from emp2 where current of c;
    end if;
  end loop;
  commit;
end;


只有注册用户登录后才能发表评论。


网站导航: