¤ë‌¼´ PL/SQL ê°•¢Œ - prof.ysu.ac. ‍료.pdf4 pl/sqlë‚´‌ sql문 4.1 insert ... ¤ë‌¼´ pl/sql ... ë³´†µ —°†

  • View
    228

  • Download
    7

Embed Size (px)

Text of ¤ë‌¼´ PL/SQL ê°•¢Œ - prof.ysu.ac....

  • PL/SQL 1

    1 PL/SQL

    1.1 PLSQL ? [ 2002/01/20 ]

    1.2 PL/SQL Block [ 2002/05/09 ]

    1.3 PL/SQL [ 2002/05/09 ]

    2 (PROCEDURE) (FUNCTION)

    2.1 (PROCEDURE) [ 2002/01/20 ]

    2.2 (FUNCTION) [ 2002/01/20 ]

    3 PL/SQL

    3.1 [ 2002/01/20 ]

    3.2

    3.3 %ROWTYPE [ 2002/01/20 ]

    3.4 PL/SQL [ 2002/01/20 ]

    3.5 PLSQL [ 2002/01/20 ]

    3.6 PL/SQL Table of Record [ 2002/01/20 ]

    4 PL/SQL SQL

    4.1 INSERT [ 2002/01/20 ]

    4.2 UPDATE [ 2002/01/20 ]

    4.3 DELETE [ 2002/01/20 ]

    5 PL/SQL

    5.1

    5.1.1 FOR LOOP [ 2002/01/20 ]

    5.1.2 LOOP, WHILE [ 2002/01/20 ]

    5.2 (IF) [ 2002/01/20 ]

    6 SQL

    6.1 (Implicit Cursor) [ 2002/01/20 ]

    6.2 (Explicit Cursor)

    6.2.1 Explicit Cursor [ 2002/01/20 ]

    6.2.2 FOR (Cursor FOR Loops) [ 2002/01/20 ]

    6.2.3 (Explicit Cursor Attributes) [ 2002/01/20 ]

    6.2.4 (Cursors with Parameters) [ 2002/01/20 ]

    6.2.5 The WHERE CURRENT OF Clause [ 2002/01/20 ]

    7

    7.1 (Exception) [ 2002/01/20 ]

    7.2 (Predefined Exceptions) [ 2002/01/20 ]

    PL/SQL

  • PL/SQL 2

    7.3 (Non-Predefined Exception) [ 2002/01/20 ]

    7.4 (User-Defined Exceptions) [ 2002/01/20 ]

    7.5 SQLCODE, SQLERRM [ 2002/01/20 ]

    8 Package() [ 2002/01/20 ]

    9 Trigger() [ 2002/01/20 ]

  • PL/SQL 3

    PL/SQL ?

    - PL/SQL Oracle's Procedural Language extension to SQL. .

    - SQL , (IF), (LOOP, WHILE, FOR) ,

    Procedure Language

    - DECLARE , .

    - PL/SQL PL/SQL .

    PL/SQL

    - PL/SQL BLOCK SQL ORACLE DB

    .

    - PL/SQL .

    - .

    - Variable, Constant, Cursor, Exception , SQL Procedural

    .

    - , .

    - DataBase .

    - Exception Oracle Server Error .

    - Exception .

    PL/SQL Block Structure

    - PL/SQL .

    - PL/SQL (), (), () ,

    BEGIN END .

    - PL/SQL .

    Declarative Section()

    - , , CURSOR, USER_DEFINE Exception

    Executable Section()

    - SQL, ,

    - BEGIN END .

    - .

    Exception Handling Section()

    - .

    - .

  • PL/SQL 4

    DECLARE

    - Optional

    - Variables, cursors, user-defined exceptions

    BEGIN

    - Mandatory

    - SQL Statements

    - PL/SQL Statements

    EXCEPTION

    - Actions to perform when errors occur

    END;

    - Mandatory

    PL/SQL

    - PL/SQL (;) . .

    - END ; .

    - PL/SQL ,

    SQL .

    - SLQ*PLUS DELCLARE BEGIN PL/SQL

    .

    - : --

    - : /* */

    - PL/SQL / .

  • PL/SQL 5

    ..

    - , PL/SQL . ..

    ..

    PL/SQL .

    CREATE OR REPLACE .

    IS PL/SQL .

    LOCAL IS BEGIN .

    [Syntax]

    CREATE OR REPLACE procedure name

    IN argument

    OUT argument

    IN OUT argument

    IS

    [ ]

    BEGIN -->

    [PL/SQL Block]

    -- SQL , PL/SQL

    [EXCEPTION] -->

    -- error

    END; -->

    SQL>CREATE OR REPLACE PROCEDURE update_sal

    /* IN Parameter */

    (v_empno IN NUMBER)

    IS

    BEGIN

    UPDATE emp

    SET sal = sal * 1.1

    WHERE empno = v_empno;

    COMMIT;

    END update_sal;

    /

    .

  • PL/SQL 6

    ..

    update_sal ..

    update_sal (v_empno) update sql .

    "/" .

    EXECUTE .

    SQL> execute update_sal(7369);

    PL/SQL .

    7369 10% .

    SELECT .

    Parameter

    program .

    .

    Parameter

    - IN : program

    - OUT : program

    - INOUT : program ,

    program

  • PL/SQL 7

    Block Type(PL/SQL )

    [ Anonymous ]

    [ Procedure ]

    [ Function ]

    Anonymous Block( )

    .

    PL/SQL .

    SQL*Plus .

    Procedure( )

    PL/SQL .

    .. ..

    PL/SQL

    .

    Function( )

    .

    IN ,

    RETURN .

    PL/SQL RETURN .

  • PL/SQL 8

    (Function)

    - .

    - IN .

    - RETURN .

    - PL/SQL RETURN .

    [Syntax]

    CREATE OR REPLACE FUNCTION function name

    [(argument...)]

    RETURN datatype

    -- Datatype datatype .

    IS

    [ ]

    BEGIN

    [PL/SQL Block]

    -- PL/SQL RETURN .

    -- PL/SQL Block .

    END;

  • PL/SQL 9

    SQL> CREATE OR REPLACE FUNCTION FC_update_sal (v_empno IN NUMBER)

    --

    RETURN NUMBER .

    IS

    v_sal emp.sal%type;

    BEGIN

    UPDATE emp SET sal = sal * 1.1 WHERE empno = v_empno;

    COMMIT;

    SELECT sal INTO v_sal FROM emp WHERE empno = v_empno;

    --

    RETURN v_sal;

    END;

    .

    ..

    v_sal %type .

    .

    (;) "/" .

    .

    SQL> VAR salary NUMBER;

    EXECUTE .

    SQL>EXECUTE :salary := FC_update_sal(7900);

    PL/SQL .

    SQL PRINT .

    PRINT salary .

    SQL>PRINT salary;

    SALARY

    ----------

    1045

    .

  • PL/SQL 10

    [Syntax]

    Identifier sql object .

    Identifier CONSTANT KEYWORD

    .

    NOT NULL ,

    .

    (:=) .

    Identifier NULL .

    Identifier .

    .

    BINARY_INTEGER -2147483647 2147483647

    NUMBER[(P, S)]

    CHAR[()] 32767 .

    1 .

    LONG 32760 .

    LONG 2147483647 .

    LONG RAW 32760Byte .

    LONG RAW PL/SQL .

    VARCHAR2() 3 32767Byte .

    VARCHAR2 .

    DATE

    .

    DATE .

    BC 4712 1 1 AD 9999 12 31 .

    BOOLEAN (TRUE, FALSE, NULL)

  • PL/SQL 11

    v_price CONTANT NUMBER(4,2) := 12.34 ; -- ( )

    v_name VARCHAR22(20) ;

    v_Bir_Type CHAR(1) ;

    v_flag BOOLEAN NOT NULL := TRUE ; -- NOT NULL TRUE

    v_birthday DATE;

    %TYPE

    %TYPE ,

    . .

    .

    %TYPE .

    %TYPE

    - DB column definition .

    - DB column definition PL/SQL .

    v_empno emp.empno%TYPE := 7900 ;

    v_ename emp.ename%TYPE;

  • PL/SQL 12

    ..

    SQL>CREATE OR REPLACE PROCEDURE Emp_Info ( p_empno IN emp.empno%TYPE )

    IS

    -- %TYPE

    v_empno emp.empno%TYPE;

    v_ename emp.ename%TYPE;

    v_sal emp.sal%TYPE;

    BEGIN

    DBMS_OUTPUT.ENABLE;

    -- %TYPE

    SELECT empno, ename, sal

    INTO v_empno, v_ename, v_sal

    FROM emp

    WHERE empno = p_empno ;

    --

    DBMS_OUTPUT.PUT_LINE( ' : ' || v_empno );

    DBMS_OUTPUT.PUT_LINE( ' : ' || v_ename );

    DBMS_OUTPUT.PUT_LINE( ' : ' || v_sal );

    END;

    /

    .

    SQL>SET SERVEROUTPUT ON; -- DBMS_OUTPUT

    SQL> EXECUTE Emp_Info(7369);

    : 7369

    : SMITH

    : 880

    PL/SQL .

  • PL/SQL 13

    .

    %ROWTYPE , PL/SQL .

    %ROWTYPE

    , , .

    %ROWTYPE .

    .

    DATATYPE .

    DATATYPE .

    %ROWTYPE ..

    SQL>CREATE OR REPLACE PROCEDURE RowType_Test ( p_empno IN emp.empno%TYPE )

    IS

    v_emp emp%ROWTYPE ;

    BEGIN

    DBMS_OUTPUT.ENABLE;

    -- %ROWTYPE

    SELECT empno, ename, hiredate

    INTO v_emp.empno, v_emp.ename, v_emp.hiredate

    FROM emp WHERE empno = p_empno;

    DBMS_OUTPUT.PUT_LINE( ' : ' || v_emp.empno );

    DBMS_OUTPUT.PUT_LINE( ' : ' || v_emp.ename );

    DBMS_OUTPUT.PUT_LINE( ' : ' || v_emp.hiredate );

    END;

    /

    .

    SQL> SET SERVEROUTPUT ON ; -- DBMS_OUTPUT.PUT_LINE

    SQL> EXECUTE RowType_Test(7900);

    : 7900

    : JAMES

    : 81/12/03

    PL/SQL .

  • PL/SQL 14

    PL/SQL

    PL/SQL SQL . PL/SQL

    .

    ROW