斷點

          每天進步一點點!
          posts - 174, comments - 56, trackbacks - 0, articles - 21

          cursor游標

          Posted on 2010-07-24 16:14 斷點 閱讀(249) 評論(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;

          主站蜘蛛池模板: 鲜城| 韶关市| 浦县| 阜平县| 安康市| 巫溪县| 琼中| 若尔盖县| 井陉县| 宣城市| 当阳市| 双峰县| 亚东县| 福贡县| 日喀则市| 文水县| 东丽区| 闸北区| 成安县| 子洲县| 吐鲁番市| 巴里| 孝义市| 博湖县| 衡南县| 夏邑县| 梅河口市| 喀喇| 新泰市| 视频| 谷城县| 乌拉特前旗| 湖北省| 噶尔县| 宁夏| 桂林市| 贞丰县| 海伦市| 武平县| 阳东县| 田阳县|