Download presentation
Presentation is loading. Please wait.
Published byDeborah Black Modified over 9 years ago
1
PL/SQL Introduction Database 1. Practice
2
Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary) Wines (name, type, country) Bars (name, street_number, street, city, tel) Likes (drinker, wine) Frequents (drinker, wine) Sells (bar, wine, price)
3
Blokk A PL/SQL program consists of blocks. A block optionally contains a declaration part, the body of the block must be given, which may be followed by an exception handling part. [DECLARE declarations] BEGIN command [commands]... [EXCEPTION exceptions] END; Note: for the use of labels and GOTO commands consult the Oracle documentation.
4
Declaration of record type Records are heterogenous, complex data types which should be always declared before their use. TYPE name IS RECORD ( field_name type [[NOT NULL] {:= | DEFAULT} expression] [, field_name type [[NOT NULL] {:= | DEFAULT} expression]…);
5
Example for a record type declaration DECLARE TYPE film IS RECORD ( titleVARCHAR2(20) NOT NULL, yearDATE, lengthNUMBER DEFAULT 90, studioVARCHAR2(15));
6
%TYPE, %ROWTYPE %TYPE : is used to refer to a previously declared type which is given to an attribute of a table, or a field of a record, or a variable. %ROWTYPE : returns the record type corresponding to the type of the rows of a table or a cursor. In both cases, whenever there is a change in the type of the referred object, in the corresponding declarations the types are automatically updated.
7
%TYPE, %ROWTYPE example CREATE TABLE Books AS ( titleVARCHAR2(50) NOT NULL, writerVARCHAR2(40) NOT NULL); DECLARE books_rowBooks%ROWTYPE, titleBooks.title%TYPE, …
8
Displaying texts DMBMS_OUTPUT package The most important procedures are – NEW_LINE – PUT – PUT_LINE Before the code of a program using the DMBMS_OUTPUT package the SET SERVEROUTPUT ON command should be issued in order to enable the displaying of texts.
9
Value assignment variable := value Conditional value assignment CASE selector WHEN condition THEN value [WHEN condition THEN value]... [ELSE value] END
10
Example for value assignments SET SERVEROUTPUT ON DECLARE v_date DATE := DATE '1983-05-29'; v_season VARCHAR2(6); BEGIN v_season := CASE WHEN TO_CHAR(v_date, 'MM') IN ('01', '02', '12') THEN 'winter' WHEN TO_CHAR(v_date, 'MM') IN ('03', '04', '05') THEN 'spring' WHEN TO_CHAR(v_date, 'MM') IN ('06', '07', '08') THEN 'summer' WHEN TO_CHAR(v_date, 'MM') IN ('09', '10', '11') THEN 'autumn' ELSE 'error' END; DBMS_OUTPUT.PUT_LINE (v_season); END;
11
SELECT INTO statement I. SET SERVEROUTPUT ON DECLARE v_occ Drinkers.occupation%TYPE; BEGIN SELECT occupation INTO v_occ FROM Drinkers WHERE name = 'Maria'; DBMS_OUTPUT.PUT_LINE('The occupation of Maria is: ' || v_occ); END; Note: if the SQL query returns more than one row or it does not return any row, then an exception is thrown.
12
SELECT INTO statement II. SET SERVEROUTPUT ON DECLARE v_occDrinkers.occupation%TYPE; v_birthDrinkers.birthday%TYPE; BEGIN SELECT occupation, birthday INTO v_occ, v_birth FROM Drinkers WHERE name = 'Maria'; DBMS_OUTPUT.PUT_LINE('Occupation: ' || v_occ || ' Birthday: ' || v_birth); END;
13
A SELECT INTO utasítás III. SET SERVEROUTPUT ON DECLARE v_rowDrinkers%ROWTYPE; BEGIN SELECT * INTO v_row FROM Drinkers WHERE name = 'Maria'; DBMS_OUTPUT.PUT_LINE('Occupation: ' || v_row.occupation || ' Birthday: ' || v_row.birthday); END;
14
IF-THEN-ELSE IF condition THEN command [command].... [ELSIF condition THEN command [command]....]... [ELSE command [command]....] END IF;
15
Example for an if-then-else statement SET SERVEROUTPUT ON DECLARE v_number NUMBER := -5; v_abs_val NUMBER; BEGIN IF v_number >= 0 THEN v_abs_val := v_number; ELSE v_abs_val := -1 * v_number; END IF; DBMS_OUTPUT.PUT_LINE ('The absolute value of: ' || v_number || ' is ' || v_abs_val ); END;
16
CASE CASE [selector] WHEN {condition | expression} THEN command [command ]... [WHEN {condition | expression} THEN command [command ]...]... [ELSE command [command ]...] END CASE;
17
Example for a CASE statement SET SERVEROUTPUT ON DECLARE mark NUMBER(1) := 4; BEGIN CASE mark WHEN 1 THEN DBMS_OUTPUT.PUT_LINE('failed'); WHEN 2 THEN DBMS_OUTPUT.PUT_LINE('bad'); WHEN 3 THEN DBMS_OUTPUT.PUT_LINE('not bad'); WHEN 4 THEN DBMS_OUTPUT.PUT_LINE('good'); WHEN 5 THEN DBMS_OUTPUT.PUT_LINE('excellent'); ELSE DBMS_OUTPUT.PUT_LINE('There is no such mark.'); END CASE; END;
18
Simple loop Syntax: LOOP command [command]... END LOOP; Example: DECLARE fakt NUMBER:=1; i PLS_INTEGER:=2; BEGIN LOOP fakt:=fakt*i; EXIT WHEN fakt > 1000; i:=i+1; END LOOP; DBMS_OUTPUT.PUT_LINE(' The factorial is: ' || fakt); END;
19
WHILE loop Syntax: WHILE condition LOOP command [command]... END LOOP; Example: DECLARE v_exp NUMBER :=1; BEGIN WHILE v_exp < 10000 LOOP v_exp := 2* v_exp; END LOOP; DBMS_OUTPUT.PUT_LINE (v_exp); END;
20
FOR loop Syntax: FOR variable IN [REVERSE] lower_bound..upper_bound LOOP command [command]... END LOOP; The type of variable is automatically set to PLS_INTEGER. By using the EXIT command one can terminate a loop statement. The program continues with the next command after the loop. In other words, the effect of EXIT is the same as that of the break statement in C++.
21
Example for a FOR statement SET SERVEROUTPUT ON DECLARE v_sum NUMBER := 0; BEGIN FOR i in 1..100 LOOP v_sum := v_sum + i; END LOOP; DBMS_OUTPUT.PUT_LINE('The sum of the first 100 numbers is: ' || v_sum); END;
22
Exercises Write a program which displays the following data: – the I love the whole world string – the country, where Cave Geisse Nature is produced – the occupation and birthday of Hannah – the number of those who visit Bar do Mineiro – the smallest number which is greater than 10000 and is divisible by 7 – those prime numbers that are less than 100.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.