Oracle_PL_ SQL_ 튜토리얼:커서

6526 단어 oracle유표plsql
--######################################--
--  
--   2012.07.06
--######################################--

/*
                        。      ,                          ,                   。             ,                。 
       :         。         SELECT...INTO...    ,               ,          DML  ,            。           ,              ,               。                  SELECT  。 
      ,                ,                     ,     。
            :  、  、  、  。
*/

--######################################--
--      
CREATE TABLE wl_emp
AS
SELECT * FROM scott.emp;
--
SELECT * FROM wl_emp;

--######################################--
--    
CREATE OR REPLACE PROCEDURE promotion_review_1
IS
    nemployeeid NUMBER;
    dstartdate  DATE;
    denddate    DATE;
    sjobid      VARCHAR2 (20);
       
    CURSOR cselectjob
    IS
        SELECT a.empno 
             , a.hiredate 
             , a.hiredate 
             , a.job
         FROM wl_emp a;
BEGIN
    OPEN cselectjob;
    LOOP
        FETCH cselectjob INTO nemployeeid
                            , dstartdate
                            , denddate
                            , sjobid ;
        EXIT WHEN cselectjob%NOTFOUND;
        DBMS_OUTPUT.put_line( 'Employee '
                              || nemployeeid
                              || ' has job '
                              || sjobid
                              || ' for '
                              || (denddate - dstartdate)
                              || ' days.'
                             );
    END LOOP;
    CLOSE cselectjob;
END;

--######################################--
--FOR  
CREATE OR REPLACE PROCEDURE promotion_review_2
IS
    CURSOR cselectjob
    IS
        SELECT a.empno
             , a.hiredate dstartdate
             , a.hiredate denddate
             , a.job
          FROM wl_emp a;
BEGIN
    FOR jh_rec IN cselectjob LOOP
        DBMS_OUTPUT.put_line( 'Employee '
                              || jh_rec.empno
                              || ' had job '
                              || jh_rec.job
                              || ' for '
                              || (jh_rec.dstartdate - jh_rec.denddate)
                              || ' days.'
                            );
    END LOOP;
END;

--######################################--
--    
/*
     %ISOPEN     FALSE,          。               SQL%attribute           。
           DML        。   INSERT、UPDATE DELETE  。
     SELECT   INTO BULK COLLECT INTO  ,     FOR       SELECT   ,         。
PL/SQL     SQL           。  DML     %ROWCOUNT  ,             。
    SELECT         。
*/
CREATE OR REPLACE PROCEDURE promotion_review_3
IS
    nempno NUMBER;
    CURSOR cselectjob
    IS
        SELECT a.empno
             , a.hiredate dstartdate
             , (a.hiredate + 1) denddate
             , a.job
          FROM wl_emp a;
BEGIN
    SELECT COUNT(*) 
      INTO nempno
      FROM wl_emp;
    
    DBMS_OUTPUT.put_line( 'There are ' 
                          || nempno
                          || ' employee history records.'
                         );
    FOR jh_rec IN cselectjob LOOP
        DBMS_OUTPUT.put_line( 'Employee '
                              || jh_rec.empno
                              || ' had job '
                              || jh_rec.job
                              || ' for '
                              || (jh_rec.denddate - jh_rec.dstartdate)
                              || ' days.'
                            );
    END LOOP;
END;

--######################################--
--REF  
--      
CREATE OR REPLACE PROCEDURE demo
IS 
    --              ,               。
    n_low_no  NUMBER := 7369;
    n_high_no NUMBER := 7654;
    emp_rec   VARCHAR2(20);    
    CURSOR c_selt_name
    IS
        SELECT a.ename
          FROM scott.emp a
         WHERE a.empno BETWEEN n_low_no AND n_high_no;
BEGIN
    OPEN c_selt_name;
    LOOP 
        FETCH c_selt_name INTO emp_rec;
        EXIT WHEN c_selt_name%NOTFOUND;
        DBMS_OUTPUT.put_line(emp_rec);
    END LOOP;
END demo;

--      
CREATE OR REPLACE PROCEDURE demo
IS  
    n_low_no  NUMBER;
    n_high_no NUMBER;
    emp_rec   VARCHAR2(20);    
    CURSOR c_selt_name
    IS
        SELECT a.ename
          FROM scott.emp a
         WHERE a.empno BETWEEN n_low_no AND n_high_no;
BEGIN
    --            。
    n_low_no  := TO_NUMBER(NVL(&1,7364));
    n_high_no := TO_NUMBER(NVL(&2,7655));
    OPEN c_selt_name;
    LOOP 
        FETCH c_selt_name INTO emp_rec;
        EXIT WHEN c_selt_name%NOTFOUND;
        DBMS_OUTPUT.put_line(emp_rec);
    END LOOP;
END demo;

--      
CREATE OR REPLACE PROCEDURE demo
IS 
    n_low_no  NUMBER;
    n_high_no NUMBER;
    emp_rec   VARCHAR2(20); 
    --              ,          。   
    CURSOR c_selt_name(low_id  NUMBER 
                     , high_id NUMBER)
    IS
        --SELECT             ,          ,             。
        SELECT a.ename
          FROM scott.emp a
         WHERE a.empno BETWEEN low_id AND high_id;
BEGIN
    --        ,  &1 &2         n_low_no n_high_no。
    n_low_no  := TO_NUMBER(NVL(&1,7364));
    n_high_no := TO_NUMBER(NVL(&2,7655));
    --                ,               low_id high_id。
    OPEN c_selt_name (n_low_no , n_high_no);
    LOOP 
        FETCH c_selt_name INTO emp_rec;
        EXIT WHEN c_selt_name%NOTFOUND;
        DBMS_OUTPUT.put_line(emp_rec);
    END LOOP;
END demo;


좋은 웹페이지 즐겨찾기