Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database 1 4 - 1 An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL.

Similar presentations


Presentation on theme: "Database 1 4 - 1 An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL."— Presentation transcript:

1 Database 1 4 - 1 An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL is an Oracle’s procedural extension of SQL, it is an advanced fourth-generation programming language (4GL)..

2 Database 1 4 - 2 Oracle PL/SQL Objective: PL/SQL PL/SQL offers modern features such as data encapsulation, overloading, collection types, exception handling, and information hiding. offers modern features such as data encapsulation, overloading, collection types, exception handling, and information hiding. improve database performance, make your applications simpler and easier to maintain for an Oracle developer or DBA. improve database performance, make your applications simpler and easier to maintain for an Oracle developer or DBA.

3 Database 1 4 - 3 Oracle PL/SQL Advantages of Oracle PLSQL PL/SQL has over other programming languages its tight coupling with the Oracle database. PL/SQL has over other programming languages its tight coupling with the Oracle database. This makes PL/SQL programs easier to write and This makes PL/SQL programs easier to write and very efficient at accessing the database. very efficient at accessing the database. Reduced Network Traffic Reduced Network Traffic

4 Database 1 4 - 4 Oracle PL/SQL Advantages of Oracle PLSQL Advantages of Oracle PLSQL PLSQL is also fully portable. You can use PLSQL is also fully portable. You can use PL/SQL on any platform which runs Oracle PL/SQL on any platform which runs Oracle and you can transfer PLSQL programs from and you can transfer PLSQL programs from one platform to another without having to one platform to another without having to recompile them. recompile them.

5 Database 1 4 - 5 Oracle PL/SQL Disadvantages of Oracle PLSQL Proprietary to Oracle Proprietary to Oracle PL/SQL is proprietary to Oracle (means if you change database then you would have to re-write all your Oracle PL/SQL programs or write your applications in a database-neutral language like Java, VB or C and use the APIs to access the database.

6 Database 1 4 - 6 Oracle PL/SQL Disadvantages of Oracle PLSQL Poor I/O Features Poor I/O Features Oracle PL/SQL has very little support for i/o either to read/write files or to read from or write to a user interface. PLSQL is designed for manipulating information from the database and not for processing files or communicating with users. Oracle PL/SQL has very little support for i/o either to read/write files or to read from or write to a user interface. PLSQL is designed for manipulating information from the database and not for processing files or communicating with users.

7 Database 1 4 - 7 PL/SQL Data Types Numeric types BINARY_INTEGER BINARY_INTEGER DEC DEC DECIMAL DECIMAL FLOAT FLOAT INT INT INTEGER INTEGER NATURAL NATURAL NUMBER NUMBER NUMERIC NUMERIC PLS_INTEGER PLS_INTEGER POSITIVE POSITIVE REAL REAL Numeric types BINARY_INTEGER BINARY_INTEGER DEC DEC DECIMAL DECIMAL FLOAT FLOAT INT INT INTEGER INTEGER NATURAL NATURAL NUMBER NUMBER NUMERIC NUMERIC PLS_INTEGER PLS_INTEGER POSITIVE POSITIVE REAL REAL Character types CHAR CHAR CHARACTER CHARACTER LONG LONG LONG RAW LONG RAW NCHAR NCHAR NVARCHAR2 NVARCHAR2 RAW RAW ROWID ROWID STRING STRING UROWID UROWID VARCHAR VARCHAR VARCHAR2 VARCHAR2 Composite Types RECORD RECORD TABLE TABLE VARRAY VARRAY LOB Types BFILE BFILE BLOB BLOB CLOB CLOB NCLOB NCLOB Reference Types REF CURSOR REF CURSOR REF object_type REF object_type Composite Types RECORD RECORD TABLE TABLE VARRAY VARRAY LOB Types BFILE BFILE BLOB BLOB CLOB CLOB NCLOB NCLOB Reference Types REF CURSOR REF CURSOR REF object_type REF object_type

8 Database 1 4 - 8 PL/SQL Data Types 1- Built-in Datatypes The built-in data types comprise integer, real (floating point), character, date/time and Boolean and support the manipulation of LOB (large object) data types. The built-in data types comprise integer, real (floating point), character, date/time and Boolean and support the manipulation of LOB (large object) data types. NUMBER(precision, scale) NUMBER(precision, scale) REAL REAL FLOAT(binary precision). FLOAT(binary precision).

9 Database 1 4 - 9 PL/SQL Data Types Character types are available as:- fixed length (CHAR(length)) fixed length (CHAR(length)) variable length (VARCHAR2(max length)) variable length (VARCHAR2(max length)) 2- User-Defined Subtypes 2- User-Defined Subtypes The standard PL/SQL data types can be extended by defining your own types in the form of records and PLSQL collections. The standard PL/SQL data types can be extended by defining your own types in the form of records and PLSQL collections.

10 Database 1 4 - 10 Oracle PL/SQL Language Elements PL/SQL the language elements consist of:- 1- loops numeric and cursor FOR loops (the latter act on the results of queries) numeric and cursor FOR loops (the latter act on the results of queries) WHILE LOOPs WHILE LOOPs Simple LOOPs Simple LOOPs

11 Database 1 4 - 11 Oracle PL/SQL Language Elements 2-Conditional and sequential control statements IF THEN and IF THEN ELSE IF THEN and IF THEN ELSE GOTO (but the use of this is usually not necessary) GOTO (but the use of this is usually not necessary) simple and searched CASE statements simple and searched CASE statements 3- Anonymous blocks 4- Named blocks PROCEDURES PROCEDURES FUNCTIONS FUNCTIONS

12 Database 1 4 - 12 Oracle PL/SQL block The general syntax is … The general syntax is …

13 Database 1 4 - 13 Oracle PL/SQL block The general syntax is... The general syntax is... DECLARATIONS DECLARATIONS BEGIN /* start of anonymous block or procedure/function */ BEGIN /* start of anonymous block or procedure/function */ EXCEPTION EXCEPTION WHEN THEN ; WHEN THEN ; END ; /*end of anonymous block or procedure/function */ END ; /*end of anonymous block or procedure/function */

14 Database 1 4 - 14 Variables and Constants Examples of variable and constant declarations: credit_limit CONSTANT NUMBER := 5000; invalid BOOLEAN := FALSE; acct_id INTEGER(4) NOT NULL DEFAULT 9999; pi CONSTANT REAL := 3.14159; postal_code VARCHAR2(20); last_name VARCHAR2(20 CHAR); my_ename emp.ename%TYPE;

15 Database 1 4 - 15 Variables and Constants %TYPE This attribute provides the datatype of a previously declared collection, cursor variable, field, object, record, database column, or variable. Example: DECLARE my_empno emp.empno%TYPE;... BEGIN my_empno := NULL;

16 Database 1 4 - 16 Variables and Constants %ROWTYPE This attribute provides a record type that represents a row in a database table or a row fetched from a previously declared cursor. Fields in the record and corresponding columns in the row have the same names and data types.

17 Database 1 4 - 17 Variables and Constants %ROWTYPE Example: DECLARE emp_rec emp%ROWTYPE; CURSOR c1 IS SELECT deptno, dname, loc FROM dept; dept_rec c1%ROWTYPE; BEGIN SELECT * INTO emp_rec FROM emp WHERE...

18 Database 1 4 - 18 PL/SQL USES TWO TYPES OF CURSORS 1- Implicit 2- Explicit. Implicitly for all SQL data manipulation statements, including queries that return only one row. Implicitly for all SQL data manipulation statements, including queries that return only one row. For queries that return more than one row, you must declare an explicit cursor, use a cursor FOR loop. 1- Implicit 2- Explicit. Implicitly for all SQL data manipulation statements, including queries that return only one row. Implicitly for all SQL data manipulation statements, including queries that return only one row. For queries that return more than one row, you must declare an explicit cursor, use a cursor FOR loop.

19 Database 1 4 - 19 Cursor declarations Cursor variable is used a to access the information after executing a multi-row query (Oracle opens an unnamed work area that stores processing information).

20 Database 1 4 - 20 Cursor declarations Examples of cursor declarations : CURSOR c1 IS SELECT empno, ename, job, sal FROM emp WHERE sal > 2000; CURSOR c2 RETURN dept%ROWTYPE IS SELECT * FROM dept WHERE deptno = 10;

21 Database 1 4 - 21CURSORCURSOR Every explicit cursor and cursor variable has four characteristics: %FOUND %FOUND %ISOPEN %ISOPEN %NOTFOUND %NOTFOUND %ROWCOUNT. %ROWCOUNT. Every explicit cursor and cursor variable has four characteristics: %FOUND %FOUND %ISOPEN %ISOPEN %NOTFOUND %NOTFOUND %ROWCOUNT. %ROWCOUNT.

22 Database 1 4 - 22 %ISOPEN This is a cursor attribute that can be appended to the name of a cursor or cursor variable. If a cursor is open, cursor_name%ISOPEN yields TRUE; otherwise, it yields FALSE.

23 Database 1 4 - 23 %NOTFOUND This is a cursor attribute that can be appended to the name of a cursor or cursor variable. Before the first fetch from an open cursor, cursor_name%NOTFOUND yields NULL. Thereafter, it yields FALSE if the last fetch returned a row, or TRUE if the last fetch failed to return a row.

24 Database 1 4 - 24 %ROWCOUNT This is a cursor attribute that can be appended to the name of a cursor or cursor variable. When a cursor is opened, %ROWCOUNT is zeroed. Before the first fetch, cursor_name%ROWCOUNT yields 0. Thereafter, it yields the number of rows fetched so far.

25 Database 1 4 - 25 declare CURSOR my_cursor IS SELECT sal *12 anual, ename FROM emp; my_rec my_cursor%ROWTYPE; BEGIN OPEN my_cursor; LOOP FETCH my_cursor INTO my_rec; EXIT WHEN my_cursor%NOTFOUND; IF my_rec.anual > 2000 THEN INSERT INTO temp VALUES (NULL, my_rec.anual, my_rec.ename); END IF; END LOOP; CLOSE my_cursor; END;declare CURSOR my_cursor IS SELECT sal *12 anual, ename FROM emp; my_rec my_cursor%ROWTYPE; BEGIN OPEN my_cursor; LOOP FETCH my_cursor INTO my_rec; EXIT WHEN my_cursor%NOTFOUND; IF my_rec.anual > 2000 THEN INSERT INTO temp VALUES (NULL, my_rec.anual, my_rec.ename); END IF; END LOOP; CLOSE my_cursor; END; Example

26 Database 1 4 - 26 Conditional Statements in PL/SQL 1)IF THEN ELSE STATEMENT IF condition THEN statement 1; ELSE statement 2; END IF;

27 Database 1 4 - 27 IF THEN ELSIF ELSE STATEMENT 2)IF condition 1 THEN 2)IF condition 1 THEN statement 1; statement 1; statement 2; statement 2; ELSIF condtion2 THEN statement 3; statement 3;ELSE statement 4; statement 4; END IF 2)IF condition 1 THEN 2)IF condition 1 THEN statement 1; statement 1; statement 2; statement 2; ELSIF condtion2 THEN statement 3; statement 3;ELSE statement 4; statement 4; END IF

28 Database 1 4 - 28 CASE statements CASE CASE WHEN x = 1 THEN sequence_of_statements_1; WHEN x = 1 THEN sequence_of_statements_1; WHEN x = 2 THEN sequence_of_statements_2; WHEN x = 2 THEN sequence_of_statements_2; WHEN x = 3 THEN sequence_of_statements_3; WHEN x = 3 THEN sequence_of_statements_3; WHEN x = 4 THEN sequence_of_statements_4; WHEN x = 4 THEN sequence_of_statements_4; WHEN x = 5 THEN sequence_of_statements_5; WHEN x = 5 THEN sequence_of_statements_5; ELSE sequence_of_statements_N; ELSE sequence_of_statements_N; END CASE; END CASE; CASE CASE WHEN x = 1 THEN sequence_of_statements_1; WHEN x = 1 THEN sequence_of_statements_1; WHEN x = 2 THEN sequence_of_statements_2; WHEN x = 2 THEN sequence_of_statements_2; WHEN x = 3 THEN sequence_of_statements_3; WHEN x = 3 THEN sequence_of_statements_3; WHEN x = 4 THEN sequence_of_statements_4; WHEN x = 4 THEN sequence_of_statements_4; WHEN x = 5 THEN sequence_of_statements_5; WHEN x = 5 THEN sequence_of_statements_5; ELSE sequence_of_statements_N; ELSE sequence_of_statements_N; END CASE; END CASE;

29 Database 1 4 - 29 CASE statement can be used with predefined selector CASE x WHEN 1 THEN sequence_of_statements_1; WHEN 1 THEN sequence_of_statements_1; WHEN 2 THEN sequence_of_statements_2; WHEN 2 THEN sequence_of_statements_2; WHEN 3 THEN sequence_of_statements_3; WHEN 3 THEN sequence_of_statements_3; WHEN 4 THEN sequence_of_statements_4; WHEN 5 THEN sequence_of_statements_5; WHEN 5 THEN sequence_of_statements_5; ELSE sequence_of_statements_N; ELSE sequence_of_statements_N; END CASE; END CASE; CASE x WHEN 1 THEN sequence_of_statements_1; WHEN 1 THEN sequence_of_statements_1; WHEN 2 THEN sequence_of_statements_2; WHEN 2 THEN sequence_of_statements_2; WHEN 3 THEN sequence_of_statements_3; WHEN 3 THEN sequence_of_statements_3; WHEN 4 THEN sequence_of_statements_4; WHEN 5 THEN sequence_of_statements_5; WHEN 5 THEN sequence_of_statements_5; ELSE sequence_of_statements_N; ELSE sequence_of_statements_N; END CASE; END CASE;

30 Database 1 4 - 30 examplesexamples CASE WHEN a < b THEN 'hello' WHEN d < e THEN 'goodbye‘ END CASE CASE WHEN supplier_name = 'IBM' and supplier_type = 'Hardware' THEN 'North office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Hardware' THEN 'North office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Software' THEN 'South office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Software' THEN 'South office‘END CASE WHEN a < b THEN 'hello' WHEN d < e THEN 'goodbye‘ END CASE CASE WHEN supplier_name = 'IBM' and supplier_type = 'Hardware' THEN 'North office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Hardware' THEN 'North office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Software' THEN 'South office‘ WHEN supplier_name = 'IBM' and supplier_type = 'Software' THEN 'South office‘END

31 Database 1 4 - 31 PL/SQ Simple LOOP The simplest form of LOOP statement is the basic (or infinite) loop, with the keywords LOOP and END LOOP, as follows: LOOP sequence_of_statements END LOOP;

32 Database 1 4 - 32 EXIT The EXIT statement forces a loop to complete unconditionally. An example follows: LOOP IF credit_rating < 3 THEN EXIT; -- exit loop immediately END IF; END LOOP; -- control resumes here

33 Database 1 4 - 33 EXIT WHEN LOOP FETCH c1 INTO... EXIT WHEN c1%NOTFOUND; -- exit loop if condition is true... END LOOP; CLOSE c1;

34 Database 1 4 - 34 PL/SQL PL/SQL WHILE-LOOP

35 Database 1 4 - 35 PL/SQL PL/SQL WHILE-LOOP An example WHILE total <= 25000 LOOP... SELECT sal INTO salary FROM emp WHERE... total := total + salary; END LOOP;

36 Database 1 4 - 36 PL/SQL PL/SQL FOR-LOOP The number of iterations through a FOR loop is known before the loop is entered. FOR loops iterate over a specified range of integers. The syntax follows: FOR counter IN [REVERSE] lower_bound..higher_bound LOOP sequence_of_statements END LOOP;

37 Database 1 4 - 37 Oracle PL/SQL for loop An example FOR i IN 1..3 LOOP -- assign the values 1,2,3 to i sequence_of_statements -- executes three times END LOOP; FOR i IN REVERSE 1..3 LOOP -- assign the values 3,2,1 to i sequence_of_statements -- executes three times END LOOP;

38 Database 1 4 - 38 PL/SQ PL/SQ CURSOR FOR Loops PL/SQ CURSOR FOR Loops The general structure is as follows: FOR IN LOOP END LOOP;

39 Database 1 4 - 39 Advantage Oracle automatically OPENs the cursor, the results are automatically FETCHed and when all rows have been returned, Oracle automatically CLOSEs the cursor for you. PL/SQ PL/SQ CURSOR FOR Loops

40 Database 1 4 - 40 PL/SQ PL/SQ CURSOR FOR Loops disadvantages If the query returns no data, then the body of the loop is not executed which causes an error. Processing is exactly the same for each row returned, which means you can't initialize variables.


Download ppt "Database 1 4 - 1 An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL."

Similar presentations


Ads by Google