Using Oracle PL/SQL

‡ PL/SQL stands for Procedural Language/SQL. ‡ PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a structural language that is more powerful than SQL. ‡ The basic unit in PL/SQL is a block. All PL/SQL programs are made up of blocks

block structure
/* Declarative section: variables, types, and local subprograms. */

/* Executable section: procedural and SQL statements go here. */ /* This is the only section of the block that is required. */

/* Exception handling section: error handling statements go here. */


Execute a PL/SQL
‡ To execute a PL/SQL program must follow the program text itself by
± A line with a single dot ("."), and then ± A line with run;

‡ with Oracle SQL programs, we can invoke a PL/SQL program by typing it in sqlplus

myBeer VARCHAR(20). .G.Variables and Types ‡ Type ± One of the types used by SQL for database columns ± A generic type used in PL/SQL such as NUMBER ± Declared to be the same as the type of some database column ‡ E. DECLARE price NUMBER.

.name%TYPE.Variables and Types ‡ %TYPE operator DECLARE myBeer Beers. ‡ %ROWTYPE operator DECLARE beerTuple Beers%ROWTYPE.

run. END. .Variables and Types ‡ ":=" operator ‡ e. BEGIN a := a + 1. .g DECLARE a NUMBER := 3.

Simple Programs in PL/SQL ‡ Plain SQL CREATE TABLE T1( e INTEGER. DELETE FROM T1. INSERT INTO T1 VALUES(2. 4). f INTEGER ). 3). T1 e 1 2 f 3 4 . INSERT INTO T1 VALUES(1.

:a). 2 4 b NUMBER. . run. . INSERT INTO T1 VALUES(:b. END.f INTO :a. 4 2 BEGIN SELECT e.Simple Programs in PL/SQL ‡ PL/SQL program T1 e f DECLARE 1 3 a NUMBER.:b FROM T1 WHERE e>1.

ELSE . ELSIF <condition_n> THEN .. ... .... ELSIF <condition_2> THEN . END IF.Control Flow in PL/SQL ‡ IF statement IF <condition_1> THEN .... ....

b NUMBER.g. . END.Control Flow in PL/SQL ‡ E. run. :b FROM T1 WHERE e>1.:a). .f INTO :a. IF b=1 THEN INSERT INTO T1 VALUES(:b. BEGIN SELECT e. END IF. DECLARE a NUMBER. ELSE INSERT INTO T1 VALUES(:b+10.:a+10).

*/ END LOOP.Control Flow in PL/SQL ‡ Loops : LOOP <loop_body> /* A list of statements. ‡ EXIT EXIT WHEN <condition>. .

EXIT WHEN i>100.‡ E. .G. DECLARE i NUMBER := 1. . END. run. i := i+1. BEGIN LOOP INSERT INTO T1 VALUES(:i. END LOOP.:i).

.<finish> LOOP <loop_body> END LOOP.Control Flow in PL/SQL ‡ WHILE loop WHILE <condition> LOOP <loop_body> END LOOP. ‡ FOR loop FOR <var> IN <start>..

END LOOP. END. 'i is odd'). x := x + 100.10 LOOP IF MOD(i.: x. COMMIT. 'i is even'). BEGIN FOR i IN 1.2) = 0 THEN -. :x. ELSE INSERT INTO temp VALUES (i. run .DECLARE x NUMBER := 100..i is even INSERT INTO temp VALUES (i. END IF. .

'Insufficient funds'). CONSTANT NUMBER(5. current balance.00. ELSE INSERT INTO temp VALUES (:acct. and message END IF.insert account.2) := 500.2). -. acct debit_amt CONSTANT NUMBER(4) := 3. . END.:debit_amt WHERE account_id = :acct.DECLARE acct_balance NUMBER(11. IF acct_balance >= debit_amt THEN UPDATE accounts SET bal = bal . :acct_balance. COMMIT. BEGIN SELECT bal INTO :acct_balance FROM accounts WHERE account_id = :acct FOR UPDATE OF bal.

ename INTO :salary.sal%TYPE. COMMIT. mgr INTO :salary. mgr_num emp. :last_name). END LOOP.DECLARE salary emp. last_name emp. :mgr_num FROM emp WHERE empno = :starting_empno. . mgr. WHILE salary < 4000 LOOP SELECT sal. BEGIN SELECT sal.mgr%TYPE.ename%TYPE. INSERT INTO temp VALUES (NULL. :salary. :last_name FROM emp WHERE empno = :mgr_num. :mgr_num. END. starting_empno CONSTANT NUMBER(4) := 7902.

Cursors ‡ Definition CURSOR T1 Cursor IS SELECT e. . f FROM T1 WHERE e < f.

.f%TYPE. */ 15) CLOSE T1Cursor.1) DECLARE /* Output variables to hold the result of the query: */ 2) a T1.e%TYPE. /* Insert the reverse tuple: */ 13) INSERT INTO T1 VALUES(b. /* If there are no more rows to fetch. 3) b T1. /* Cursor declaration: line 4-8*/ 4) BEGIN 9) OPEN T1Cursor. 17) . 10) LOOP /* Retrieve each row of the result of the above query into PL/SQL variables: */ 11) FETCH T1Cursor INTO a. b. 16) END. 18) run. a). exit the loop: */ 12) EXIT WHEN T1Cursor%NOTFOUND. /* Free cursor used by the query. 14) END LOOP.

ename). ename FROM emp. my_rec. 0) wages. . IF my_rec. BEGIN OPEN my_cursor.wages.DECLARE CURSOR my_cursor IS SELECT sal + NVL(comm. LOOP FETCH my_cursor INTO :my_rec.wages > 2000 THEN INSERT INTO temp VALUES (NULL. my_rec. CLOSE my_cursor. EXIT WHEN my_cursor% NOTFOUND. my_rec my_cursor%ROWTYPE. END IF. END. END LOOP.

OPEN num2_cur. pair_num NUMBER := 0. BEGIN OPEN num1_cur. CLOSE num2_cur.loop through the two tables and get -.num%TYPE. CLOSE num1_cur. FETCH num2_cur INTO :num2. LOOP -. END. num1 num1_tab. END LOOP. . num2 num2_tab. CURSOR num2_cur IS SELECT num FROM num2_tab ORDER BY sequence. pair_num := pair_num + 1.DECLARE CURSOR num1_cur IS SELECT num FROM num1_tab ORDER BY sequence.pairs of numbers FETCH num1_cur INTO :num1. EXIT WHEN (num1_cur%NOTFOUND) OR (num2_cur%NOTFOUND). INSERT INTO sum_tab VALUES (pair_num. num1 + num2).num%TYPE.

my_ename). empno. LOOP FETCH c1 INTO :my_ename. INSERT INTO temp VALUES (my_sal. :my_sal. -. my_empno NUMBER(4). CLOSE c1. my_sal NUMBER(7. END LOOP. EXIT WHEN (c1%ROWCOUNT > 5) OR (c1%NOTFOUND).2).start with highest-paid employee my_ename CHAR(10). sal FROM emp ORDER BY sal DESC. COMMIT. my_empno. . BEGIN OPEN c1.DECLARE CURSOR c1 is SELECT ename. END. :my_empno.

BEGIN OPEN c1. n3 FROM data_table WHERE exper_num = 1. -. LOOP FETCH c1 INTO :num1. END. CLOSE be of same type as num3 data_table.the c1%NOTFOUND condition evaluates TRUE when FETCH finds no more rows /* calculate and store the results */ result := num2/(num1 + num3). -.n2%TYPE. n2. :num3. COMMIT. EXIT WHEN c1%NOTFOUND. NULL. -.database columns result temp. CURSOR c1 IS SELECT n1.num_col1%TYPE. END LOOP. NULL). INSERT INTO temp VALUES (result.n1%TYPE.DECLARE num1 data_table. -. :num2.Declare variables num2 data_table. .


DBMS_OUTPUT. SYSDATE). Empadd. DBMS_OUTPUT.PUT(' And Add Details For '||RTRIM(Empname)).NEW_LINE(). DBMS_OUTPUT. /* Increment Department strength */ Increment_Dept_Strength(Deptcd). Deptcd.I/O Example BEGIN /* Get Current User Name */ SELECT username INTO :l_current_user FROM USER_USERS. DBMS_OUTPUT. END Insert_Emp_Details.PUT_LINE(' having Id '||RTRIM(empid)). . INSERT INTO Employee_table VALUES(Empid. Grade. Empname.PUT('Connect As '||l_current_user).

.Logging In to Oracle ‡ log in to Oracle by typing: sqlplus <yourName> ‡ Changing Your Password alter user <yourName identified by <newPassword.

.Quitting sqlplus ‡ To leave sqlplus. type quit.

Executing SQL From a File ‡ Executing SQL From a File sqlplus <yourName/<yourPassword @<fileName ‡ e.g. sqlplus sally/etaoinshrdlu @foo OR @foo.sql .

and makes line n the current line ‡ L m n prints lines m through n. and makes the last line in the buffer the "current" line ‡Ln prints line n of the command buffer.Editing Commands ‡ L lists the command buffer. and makes line n the current line .

Editing Commands .cont. you must terminate the sequence of new lines with a pair of "returns" ‡ C /old/new replaces the text "old" by "new" in the current line ‡ A textappends "text" to the end of the current line ‡ DEL deletes the current line . ‡ I enters a mode that allows you to input text following the current line.

5 using PL/SQL .‡ Exercises 4.

Sign up to vote on this title
UsefulNot useful