************** ºÎ¼­/Á÷¿ø ¸®Æ÷Æ® ******************

ºÎ¼­ : ºÎ¼­¹øÈ£       ºÎ¼­¸í             À§Ä¡

        10         ACCOUNTING        NEW YORK

»ç¿ø : »ç¹ø     À̸§         ±Þ¿©          ±Þ¿© µî±Þ

       7871    Anderson    2000         ***   
      7890    Andrew       700         * 
      7870    Scott       2400         **** 
      7866    David        900         * 
      7891    Sylvia      3200         *****

2¸íÀÇ ±Þ¿©°¡ ÀλóµÇ¾ú½À´Ï´Ù.


À§¿Í °°Àº ¸®Æ÷Æ®¸¦ »Ì¾Æ³»°í $1000º¸´Ù ÀûÀº ±Þ¿©¸¦ ¹Þ´Â Á÷¿øÀÇ ±Þ¿©¸¦ 10%ÀλóÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇÏ¿© º¾´Ï´Ù.

ACCEPT p_deptno PROMPT 'Please enter the department number : '

DECLARE

   v_deptno    NUMBER(2);

   v_dname     CHAR(14);

   v_loc       CHAR(13);

   v_empno     emp.empno%TYPE;

   v_ename     emp.ename%TYPE;

   v_sal       emp.sal%TYPE;

   v_grade     salgrade.grade%TYPE;

   v_dno       emp.deptno%TYPE := &p_deptno;

   CURSOR emp_cursor IS

          SELECLT empno, ename, sal

          FROM emp

          WHERE deptno = &p_deptno;

   e_no_emp    EXCEPTION;

BEGIN

   -- print out the report title

   DBMS_OUTPUT.PUT_LINE('*********ºÎ¼­/Á÷¿ø ¸®Æ÷Æ®*********');

   SELECT  deptno, dname, loc

      INTO v_deptno, v_dname, v_loc

      FROM dept

      WHERE   deptno = v_dno;

   DBMS_OUTPUT.PUT_LINE('ºÎ¼­ :  ºÎ¼­¹øÈ£   ºÎ¼­¸í        À§Ä¡');

   DBMS_OUTPUT.PUT_LINE('     '¡«v_deptno¡«'    '¡«v_dname¡«
                        '   '¡«v_loc¡«'   ');

   DBMS_OUTPUT.PUT_LINE('»ç¿ø : »ç¹ø   À̸§   ±Þ¿©    ±Þ¿©µî±Þ');

   OPEN emp_cursor;

   FETCH emp_cursor INTO v_empno, v_ename, v_sal;

   /* according to the salary amount,

      print out the stars */

   WHILE emp_cursor%FOUND LOOP

      SELECT grade

         INTO v_grade

         FROM salgrade

         WHERE v_sal > losal AND v_sal <hisal;

      DBMS_OUTPUT.PUT('      '¡«v_empno¡«'   '¡«v_ename¡«
                      '      '¡«v_sal¡«'     ');

      FOR I in 1..v_grade LOOP

         DBMS_OUTPUT.PUT('*');

      END LOOP;

      DBMS_OUTPUT.PUT_LINE('');

      FETCH emp_cursor INTO v_empno, v_ename, v_sal;

   END LOOP;

   CLOSE emp_cursor;

   UPDATE emp

      SET     sal = sal * 1.1

      WHERE   sal < 1000;

   IF SQL%ROWCOUNT = 0 THEN

      RAISE e_no_emp;

   ELSE

      DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT ¡«'¸íÀÇ ±Þ¿©°¡ ' ||
                           'ÀλóµÇ¾ú½À´Ï´Ù.');

   END IF;

   COMMIT;

EXCEPTION

   WHEN no_data_found THEN

      DBMS_OUTPUT.PUT_LINE('There is no matching data.');

   WHEN too_many_rows THEN

      DBMS_OUTPUT.PUT_LINE('More than one matching data. ');

   WHEN e_no_emp THEN

      DBMS_OUTPUT.PUT_LINE('No employee with a smaller ' ||
                           'than  $1000.');

   --in case of any other exception

   WHEN others THEN

      DBMS_OUTPUT.PUT_LINE('Some other error occurred.');

END;  

 


PL/SQL º¯¼ö ¼±¾ð

  PL/SQL ºí·Ï¿¡¼­ ±×°ÍµéÀ» ÂüÁ¶Çϱâ Àü¿¡ ¼±¾ð¼½¼Ç¿¡¼­ ¸ðµç PL/SQL ½Äº°ÀÚ¸¦   ¼±¾ðÇÒ ÇÊ¿ä°¡ ÀÖ½À´Ï´Ù. ÃʱⰪÀ» ÇÒ´çÇϱâ À§ÇØ ¿É¼ÇÀ» °¡Áý´Ï´Ù. º¯¼ö¸¦ ¼±¾ð   Çϱâ À§ÇØ º¯¼ö¿¡ ´ëÇÑ °ªÀ» ÇÒ´çÇÒ ÇÊ¿ä´Â ¾ø½À´Ï´Ù. ¼±¾ð¿¡¼­ ´Ù¸¥ º¯¼ö¸¦ ÂüÁ¶   ÇÑ´Ù¸é, ÀÌÀü ¹®Àå¿¡¼­ °³º°ÀûÀ¸·Î ±×°ÍµéÀ» ¹Ýµå½Ã ¼±¾ðÇØ ³õ¾Æ¾ß ÇÕ´Ï´Ù.
  
  ±¸¹®¿¡¼­
       
         identifier            º¯¼öÀÇ À̸§ÀÔ´Ï´Ù.
        
         CONSTANT        º¯¼ö¸¦ º¯°æÇÒ ¼ö ¾øµµ·Ï Çϱâ À§ÇØ º¯¼ö¸¦ Á¦¾àÇÕ´Ï´Ù.                                   »ó¼ö´Â ÃʱâÈ­  µÇ¾î¾ß ÇÕ´Ï´Ù.
        
         datatype            ½ºÄ®¶ó,Á¶ÇÕ, ÂüÁ¶, LOB µ¥ÀÌÅÍÇü ÀÔ´Ï´Ù. (ÀÌÀå¿¡¼­´Â                                   ½ºÄ®¶ó¿Í Á¶ÇÕ µ¥ÀÌÅÍÇü¸¸ ³íÇÕ´Ï´Ù).
        
         NOT NULL         °ªÀ» Æ÷ÇÔÇؾ߸¸ Çϵµ·Ï Çϱâ À§ÇØ º¯¼ö¸¦ Á¦¾àÇÕ´Ï´Ù. NOT                                   NULLº¯¼ö´Â ÃʱâÈ­µÇ¾î¾ß ÇÕ´Ï´Ù.
        
         expr                  ¸®ÅÍ·², ´Ù¸¥ º¯¼ö, ¶Ç´Â ¿¬»êÀÚ³ª ÇÔ¼ö¸¦ Æ÷ÇÔÇϴ ǥÇöÀÌ                                   µÉ ¼ö Àִ PL/SQL Ç¥Çö½ÄÀÔ´Ï´Ù.

 

½Ç½À

1. ¾Æ·¡  ¼±¾ð¹® °¢°¢À» Æò°¡ÇÕ´Ï´Ù. À̰͵é Áß ¾î¶² °ÍÀÌ ¿Ã¹Ù¸£Áö ¾ÊÀ¸¸ç ¿Ö ±×·±°¡¸¦  ¼³¸íÇϽʽÿÀ.
 

a.

DECLARE
   v_id             NUMBER(4);


______________________________________________________________________
  

b.

DECLARE
   v_x, v_y, v_z    VARCHAR2(10);


______________________________________________________________________
 

c.

DECLARE
   v_birthdate      DATE NOT NULL;

 

º¯¼ö °ª ÁöÁ¤

º¯¼ö °ªÀ» ÁöÁ¤Çϰųª ÀçÁöÁ¤Çϱâ À§ÇØ, PL/SQL ÁöÁ¤ ¹®ÀåÀ» ¾¹´Ï´Ù. ÁöÁ¤(assignment) ¿¬»êÀÚ (:=)ÀÇ ÁÂÃø¿¡ »õ °ªÀ» ¹Þ±â À§ÇÑ º¯¼ö¸¦ Àû½À´Ï´Ù.
±¸¹®¿¡¼­,
       
        identifier    ½ºÄ®¶ó º¯¼öÀÇ À̸§ÀÔ´Ï´Ù.
       
        expr          º¯¼ö, ¸®ÅÍ·², ÇÔ¼ö È£ÃâÀÌ µÉ ¼ö ÀÖÁö¸¸ µ¥ÀÌÅͺ£À̽º ¿­Àº ¾ÈµË´Ï´Ù.

ÁöÁ¤ º¯¼ö °ª ¿¹´Â ¾Æ·¡¿Í °°ÀÌ Á¤Àǵ˴ϴÙ:

º¯¼ö °ªÀ» ÁöÁ¤Çϱâ À§ÇÑ ´Ù¸¥ ¹æ¹ýÀº µ¥ÀÌÅͺ£À̽º °ª¿¡¼­ ÀÎÃâÇϰųª ¼±ÅÃÇÏ´Â °ÍÀÔ´Ï´Ù. ¾Æ·¡ ¿¹¿¡¼­, Á¾¾÷¿øÀÇ ±Þ¿©¸¦ ¼±ÅÃÇÒ ¶§ 10% º¸³Ê½º¸¦ °è»êÇϵµ·Ï ÇÕ´Ï´Ù. 

SQL> SELECT     sal * 0.10
  2  INTO       bonus
  3  FROM       emp
  4  WHERE      empno = 7369;


À̶§ ´Ù¸¥ °è»ê¿¡¼­ º¯¼ö bonus¸¦ »ç¿ëÇÒ ¼ö Àְųª ±× °ªÀ» µ¥ÀÌÅͺ£À̽º Å×ÀÌºí¿¡ »ðÀÔÇÒ ¼ö ÀÖ½À´Ï´Ù.

ÁÖ: µ¥ÀÌÅͺ£À̽º¿¡¼­ÀÇ °ªÀ» º¯¼ö·Î ÁöÁ¤Çϱâ À§ÇØ,  SELECT ³ª FETCH ¹®ÀåÀ» »ç¿ëÇÕ´Ï´Ù.

º¯¼ö´Â ºí·ÏÀ̳ª ¼­ºêÇÁ·Î±×·¥ ¾ÈÀ¸·Î µé¾î°¥ ¶§¸¶´Ù ÃʱâÈ­µË´Ï´Ù. µðÆúÆ®·Î º¯¼ö´Â NULL·Î ÃʱâÈ­µË´Ï´Ù. ¸íÈ®ÇÏ°Ô º¯¼ö¸¦ ÃʱâÈ­ÇÏÁö ¾ÊÀ¸¸é, °ªÀº Á¤ÀǵÇÁö ¾Ê½À´Ï´Ù.

- ƯÁ¤ °ªÀÌ ¾ø´Â º¯¼ö¿¡ ´ëÇØ ÁöÁ¤ ¿¬»êÀÚ(:=)¸¦ »ç¿ëÇÕ´Ï´Ù.


    v_hiredate := to_date('15-SEP-99', 'DD-MON-YY');

 


µ¥ÀÌÅÍÇü

¼³  ¸í

VARCHAR2 (maximum_length)

 º¯¼ö ±æÀÌ ¹®ÀÚ µ¥ÀÌÅÍ¿¡ ´ëÇÑ ±âº»ÇüÀº 32767  ¹ÙÀÌÆ®  ±îÁö ÀÔ´Ï´Ù.   VARCHAR2 º¯¼ö¿Í »ó¼ö¿¡ ´ëÇÑ  µðÆúÆ®  Å©±â´Â ¾ø½À´Ï´Ù.

NUMBER
[(precision, scale)]

 °íÁ¤(fixed)°ú À¯µ¿(floating) Æ÷ÀÎÆ® ¼ýÀÚ¿¡ ´ëÇÑ ±âº»Çü

DATE

 ³¯Â¥¿Í ½Ã°£¿¡ ´ëÇÑ ±âº»Çü. DATE °ªÀº ÀÚÁ¤ ÀÌÈÄÀÇ ÃÊ  ´ÜÀ§·Î ³¯¿¡ ´ëÇÑ ½Ã°£À» Æ÷ÇÔÇÕ´Ï´Ù. ³¯Â¥ÀÇ ¹üÀ§´Â BC  4712¿Í AD 9999»çÀÌ ÀÔ
 ´Ï´Ù. 

CHAR [(maximum_length)]

 °íÁ¤ ±æÀÌ ¹®ÀÚ µ¥ÀÌÅÍ¿¡ ´ëÇÑ ±âº»ÇüÀº 32767 ¹ÙÀÌÆ®  ±îÁöÀÔ´Ï´Ù.
 maximum_length¸¦ ÁöÁ¤ÇÏÁö ¾Ê´Â´Ù¸é µðÆúÆ® ±æÀÌ´Â    1 ·Î ¼³Á¤µË´Ï´Ù 

LONG

 °íÁ¤ ±æÀÌ ¹®ÀÚ µ¥ÀÌÅÍ¿¡ ´ëÇÑ ±âº»ÇüÀº 32760 ¹ÙÀÌÆ®  ±îÁö ÀÔ´Ï´Ù.           LONG µ¥ÀÌÅͺ£À̽º ¿­ÀÇ ÃÖ´ë ÆøÀº  2147483647 ¹ÙÀÌÆ®ÀÔ´Ï´Ù. 

LONG RAW

 ÀÌÁø  µ¥ÀÌÅÍ¿Í ¹ÙÀÌÆ® ¹®ÀÚ¿­¿¡ ´ëÇÑ ±âº»ÇüÀº 32760  ¹ÙÀÌÆ®±îÁö ÀÔ´Ï´Ù.  LONG RAW µ¥ÀÌÅÍ´Â PL/SQL¿¡  ÀÇÇØ Çؼ®µÇÁö ¾Ê½À´Ï´Ù. 

BOOLEAN

 ³í¸®Àû °è»ê¿¡ »ç¿ëµÇ´Â 3°¡Áö °¡´ÉÇÑ °ª °¡¿îµ¥ ±âº»Çü  TRUE, FALSE, NULL. 

BINARY_INTEGER

 -2147483647 °ú 2147483647»çÀÌÀÇ Á¤¼ö¿¡ ´ëÇÑ ±âº»Çü.

PLS_INTEGER

 -2147483647 °ú 2147483647»çÀÌÀÇ signed Á¤¼ö¿¡ ´ëÇÑ  ±âº»Çü.
 PLS_INTEGER °ªÀº  NUMBER ¿Í  BINARY_INTEGER  °ªº¸´Ù ÀûÀº ±â¾ïÀåÄ¡¸¦ ÇÊ¿ä·Î ÇÕ´Ï´Ù. 


ÁÖ: LONG µ¥ÀÌÅÍÇüÀº LONG °ªÀÇ ÃÖ´ë ±æÀÌ°¡ 32760 ¹ÙÀÌÆ®ÀÎ °ÍÀ» Á¦¿ÜÇÏ°í´Â VARCHAR2¿Í À¯»çÇÕ´Ï´Ù. ±×·¯¹Ç·Î 32760 ¹ÙÀÌÆ®º¸´Ù ´õ ±ä °ªÀº LONG µ¥ÀÌÅͺ£À̽º ¿­¿¡¼­ LONG PL/SQLº¯¼ö·Î ¼±ÅÃµÉ ¼ö ¾ø½À´Ï´Ù.

½Ç½À(°è¼Ó)

2. °¢ ¾Æ·¡ ÁöÁ¤¿¡¼­ °á°ú Ç¥Çö½ÄÀÇ µ¥ÀÌÅÍ ÇüÀ» °áÁ¤ÇÕ´Ï´Ù.
 

a.

v_days_to_go := v_due_date - SYSDATE; 


______________________________________________________________________
  

b.

v_sender := USER || ': ' || TO_CHAR(v_dept_no);


______________________________________________________________________
 

c.

v_sum := $100,000 + $250,000;


______________________________________________________________________
 

d.

v_flag := TRUE;


______________________________________________________________________
 

e.

v_n1 := v_n2 > (2 * v_n3);


______________________________________________________________________
 

 f.

v_value := NULL;

 

%TYPE ¼Ó¼º°ú º¯¼ö¼±¾ð
 
 Á¾¾÷¿øÀÇ À̸§À» ÀúÀåÇϱâ À§ÇØ º¯¼ö¸¦ ¼±¾ðÇÕ´Ï´Ù.

  ...
  v_ename         emp.ename%TYPE;
  ...


10ÀÎ ÃÖ¼Ò Àܾװú ÀºÇà °èÁ¤ÀÇ Àܾ×À» ÀúÀåÇϱâ À§ÇØ º¯¼ö¸¦ ¼±¾ðÇÕ´Ï´Ù.

  ...
  v_balance       NUMBER(7,2);
  v_min_balance   v_balance%TYPE := 10;
  ...


NOT NULL ¿­ Á¦¾àÁ¶°ÇÀº  %TYPEÀ» »ç¿ëÇÏ¿© ¼±¾ðµÈ º¯¼ö¿¡ ´ëÇؼ­´Â Àû¿ëµÇÁö ¾È½À´Ï´Ù.±×·¯¹Ç·Î  NOT NULL ·Î Á¤ÀÇµÈ µ¥ÀÌÅͺ£À̽º ¿­À» »ç¿ëÇÏ¿©  %TYPE ¼Ó¼ºÀ» »ç¿ëÇÏ´Â º¯¼ö¸¦ ¼±¾ðÇÑ´Ù¸é º¯¼ö¿¡  NULL°ªÀ» ÁöÁ¤ÇÒ ¼ö ÀÖ½À´Ï´Ù.

 


LOB µ¥ÀÌÅÍÇü º¯¼ö

LOB (large object) Oracle8 µ¥ÀÌÅÍÇüÀ¸·Î ±¸Á¶È­µÇÁö ¾ÊÀº µ¥ÀÌÅÍ(ÅؽºÆ®, ±×·¡ÇÈÀ̹ÌÁö, ºñµð¿À Ŭ¸³, ¼Ò¸® ¿þÀ̺ê Æû °°Àº)ºí·ÏÀ» 4±â°¡ ¹ÙÀÌÆ® Å©±â±îÁö ÀúÀåÇÒ ¼ö ÀÖ½À´Ï´Ù. LOB µ¥ÀÌÅÍÇüÀº µ¥ÀÌÅÍ¿¡ ´ëÇÑ ·£´ý ¾×¼¼½º¸¦ Áö¿øÇÕ´Ï´Ù.

  • CLOB (character large object) µ¥ÀÌÅÍÇüÀº µ¥ÀÌÅͺ£À̽º ³»ÀÇ ´ÜÀÏ ¹ÙÀÌÆ® ¹®ÀÚ µ¥ÀÌÅÍÀÇ ´ëÇü ºí·ÏÀ» ÀúÀåÇϱâ À§ÇØ »ç¿ëµË´Ï´Ù.
  • BLOB (binary large object)  µ¥ÀÌÅÍÇüÀº ÇàÀÇ ¾ÈÆÆ¿¡ µ¥ÀÌÅÍ º£À̽º ³»ÀÇ ´ëÇü  ÀÌÁø °´Ã¼¸¦ ÀúÀåÇϱâ À§ÇØ »ç¿ëµË´Ï´Ù.
  •  BFILE (binary file)  µ¥ÀÌÅÍÇüÀº µ¥ÀÌÅÍ º£À̽º ¿ÜºÎÀÇ ¿î¿µ ½Ã½ºÅÛ ÆÄÀÏÀÇ ´ëÇü  ÀÌÁø °´Ã¼¸¦ ÀúÀåÇϱâ À§ÇØ »ç¿ëµË´Ï´Ù.
  • NCLOB (national language character large object) µ¥ÀÌÅÍÇüÀº µ¥ÀÌÅÍÀ̽º ³»ÀÇ ´ÜÀÏ ¹ÙÀÌÆ®,¶Ç´Â °íÁ¤ ±æÀÌÀÇ ¸ÖƼ¹ÙÀÌÆ® NCHAR µ¥ÀÌÅ͸¦ ÇàÀÇ ¾ÈÆÆ¿¡ ÀúÀåÇϱâ À§ÇØ »ç¿ëµË´Ï´Ù.

º¯¼ö ÁöÁ¤°ª

È£½ºÆ® º¯¼ö¸¦ ÂüÁ¶Çϱâ À§ÇØ, ¼±¾ðµÈ PL/SQL º¯¼ö¿Í È£½ºÆ® º¯¼ö¸¦ ±¸º°Çϱâ À§ÇØ ÄÝ·Ð(:)À¸·Î ÂüÁ¶ Á¢µÎ¾î¸¦ ¸¸µé¾î¾ß ÇÕ´Ï´Ù.

¿¹

 :host_var1 := v_sal;
 :global_var1 := 'YES'

 
¿¹

 SQL> VARIABLE  return_code NUMBER
 ... :return_code := 10 ;
 SQL> PRINT return_code

 ´ÙÀ½ Áß ¿Ã¹Ù¸¥ PL/SQL ¹®ÀåÀÌ ¾Æ´Ñ °ÍÀº?
A. v_ename := LOWER(v_ename);
B. v_total := SUM(number_total);
C. v_comment := USER||': '||TO_CHAR(SYSDATE);
D. v_equal := (v_empno IS NOT NULL);
´ä

    X Á¤´ä:B


  1. ´ÙÀ½ÀÇ PL/SQL ºí·ÏÀ» ½ÇÇàÇÑ °á°ú¸¦ ¿Ã¹Ù¸£°Ô ±â¼úÇÑ °ÍÀº?
    DECLARE
        v_weight NUMBER(3):=600;
        v_message VARCHAR2(255) := 'Product 10012';
    BEGIN
        DECLARE
            v_weight NUMBER(3):=1;
            v_message VARCHAR2(255):='Product 11001';
            v_new_locn VARCHAR2(50):='Europe';
        BEGIN
            v_weight:=v_weight+1;
            v_new_locn:='Western '||v_new_locn;
        END;
        v_weight:=v_weight+1;
        v_message:=v_message ||' is in stock';
        v_new_locn:='Western '||v_new_locn;
    END;
    A. main blockÀÇ v_weightÀÇ °ªÀº 602ÀÌ´Ù.
    B. sub blockÀÇ v_weightÀÇ °ªÀº 601ÀÌ´Ù.
    C. main blockÀÇ v_new_locnÀÇ °ªÀº 'Western Europe'ÀÌ´Ù.
    D. main blockÀÇ v_messageÀÇ °ªÀº 'Product 10012 is in stock'ÀÌ´Ù.
    ´ä
    X Á¤´ä:D