Download presentation
Presentation is loading. Please wait.
Published byMya Baum Modified over 9 years ago
1
PL/SQL
2
Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g. Forms, Reports to create triggers, procedures, functions, etc.). PL/SQL provides high-level language features such as block structure, conditional statements, loop statements, variable types, structured data and customized error handling PL/SQL is integrated with the database server. It does not exist as a standalone language.
3
Basic Structure of PL/SQL The basic unit in PL/SQL is a block. All PL/SQL programs are made up of blocks, which can be nested within each other.
4
DECLARE /* Declarative section: variables, types, and local subprograms. */ BEGIN /* Executable section: procedural and SQL statements go here. */ /* This section of the block is required. */ EXCEPTION /* Exception handling section: error handling statements go here. */ END;
5
Only the executable section is required. The other sections are optional. The only SQL statements allowed in a PL/SQL program are SELECT, INSERT, UPDATE, DELETE and several other data manipulation statements plus some transaction control. Data definition statements like CREATE, DROP, or ALTER are not allowed.
6
The executable section also contains constructs such as assignments, branches, loops, procedure calls, and triggers. PL/SQL is not case sensitive. C style comments (/*... */) may be used, or for a one line comment precede it with (--). To execute a PL/SQL program, we must follow the program text itself by A line with a single dot ("."), and then A line with run;
7
Variables and Types Types in PL/SQL are the same as in SQL, in addition to the BOOLEAN type. Variables can be declared in the following ways: 1.Declare id NUMBER; name VARCHAR(20);
8
2.Declare id emp.empno%TYPE; name emp.ename%TYPE; variable name table name column name
9
3.Declare depttuple dept%ROWTYPE; depttuple will be a record that contains fields that represent the columns in table dept. depttuple(deptno,dname,location)
10
A variable can be intialized: Declare a NUMBER :=3; A variable can be constrained to not null: Declare a NUMBER NOT NULL:=3; Or a NUMBER NOT NULL DEFAULT 3;
11
Select statement Select statement has a different form SELECT column INTO variables FROM table WHERE condition; Select should return a single tuple, if several tuples are required, use a Cursor
12
Simple program Using the following table declaration: CREATE TABLE T1( e INTEGER, f INTEGER ); INSERT INTO T1 VALUES(1, 3); INSERT INTO T1 VALUES(2, 4);
13
DECLARE a NUMBER; b NUMBER; BEGIN SELECT e,f INTO a,b FROM T1 WHERE e>1; INSERT INTO T1 VALUES(b,a); END; ef 13 24 ef 13 24 42
14
Control Flow in PL/SQL IF-THEN-END-IF LOOP-EXIT WHEN-END LOOP FOR-END LOOP WHILE-END LOOP GOTO
15
IF-THEN-END-IF IF THEN ELSE END IF; IF THEN... ELSIF THEN......... ELSIF THEN... ELSE... END IF;
16
DECLARE a NUMBER; b NUMBER; BEGIN SELECT e,f INTO a,b FROM T1 WHERE e>1; IF b=1 THEN INSERT INTO T1 VALUES(b,a); ELSE INSERT INTO T1 VALUES(b+10,a+10); END IF; END;
17
LOOP-EXIT WHEN-END LOOP LOOP /* A list of statements. */ EXIT WHEN ; /*list of statements*/ END LOOP;
18
Insert each of the pairs (1, 1) through (100, 100) into T1 DECLARE i NUMBER := 1; BEGIN LOOP INSERT INTO T1 VALUES(i,i); i := i+1; EXIT WHEN i>100; END LOOP; END;
19
While - For WHILE LOOP END LOOP; FOR IN [REVERSE].. LOOP END LOOP; -- will be declared implicitly
20
Cursors A cursor is a variable that runs through the tuples of some relation. By fetching into the cursor each tuple of the relation, we can write a program to read and process the value of each such tuple. Ex: Delete every tuple whose first component is less than the second, and insert the reverse tuple into T1.
21
DECLARE /* Output variables to hold the result of the query: */ a T1.e%TYPE; b T1.f%TYPE; /* Cursor declaration: */ CURSOR T1Cursor IS SELECT e,f FROM T1 WHERE e < f FOR UPDATE;
22
BEGIN OPEN T1Cursor; LOOP /* Retrieve each row of the result of the above query into PL/SQL variables: */ FETCH T1Cursor INTO a, b; /* If there are no more rows to fetch, exit the loop: */ EXIT WHEN T1Cursor%NOTFOUND; /* Delete the current tuple: */ DELETE FROM T1 WHERE CURRENT OF T1Cursor; /* Insert the reverse tuple: */ INSERT INTO T1 VALUES(b, a); END LOOP; /* Free cursor used by the query. */ CLOSE T1Cursor; END;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.