Professional Documents
Culture Documents
FEATURES
Conditional Control
Iterative Control Cursor
[DECLARE
Declaring Variables
Variables can have any SQL datatype, such as CHAR, DATE, or NUMBER, or aPL/SQL-only datatype, such as BOOLEAN or PLS_INTEGER. Example:
DECLARE part_no NUMBER(6); part_name VARCHAR2(20); in_stock BOOLEAN; part_price NUMBER(6,2);
Can also declare nested tables, variable-size arrays, and records using the TABLE, VARRAY, and RECORD composite datatypes.
DECLARING VARIABLES
%TYPE
DECLARING VARIABLES
%ROWTYPE
%ROWTYPE attribute provides a record type that represents a row in a table. Columns in a row and corresponding fields in a record have the same names and datatypes. Example:
DECLARE dept_rec departments%ROWTYPE; BEGIN SELECT * INTO dept_rec FROM departments; DBMS_OUTPUT.PUT_LINE(dept_rec.deptname); END
&VARIABLE
Is a bind variable. Used to take value from user at run time. Eg.
DECLARE
empid number; emp_rec employee%ROWTYPE;
BEGIN
SELECT * INTO emp_rec FROM employee WHERE emp_id=&empid; DBMS_OUTPUT.PUT_LINE(emp_rec.ename);
END
Example:
DECLARE bonus NUMBER(8,2); emp_id NUMBER(6) := 100; BEGIN SELECT salary * 0.10 INTO bonus FROM employees WHERE employee_id = emp_id; END;
3. By passing variable as an OUT or IN OUT parameter to subprogram, then assigning value Declaring Constants
o
Example:
o
BIND VARIABLES
You do need to specify bind variables with dynamic SQL i.e. bind variables are used to take value from user at run time Syntax
CONDITIONAL CONTROL
IF statement:
Syntax:
IF condition THEN Statements; [ELSEIF condition THEN Statements; [ELSE Statements;]] END IF
CONDITIONAL CONTROL
CASE Statement:
Syntax:
CASE
WHEN condition THEN Statements; .[n] ELSE Statements;
ITERATIVE CONTROL
Endless LOOP:
LOOP
-- sequence of statements
END LOOP;
For endless loop to terminate, you must write EXIT WHEN statement. Eg.
LOOP emp_id:=emp_id+1; EXIT WHEN empid > 10; END LOOP
ITERATIVE CONTROL
WHILE LOOP:
Syantax:
WHILE condition LOOP Statements; END LOOP
Eg.
WHILE emp_id < 10 LOOP DBMS_OUTPUT.PUT_LINE(emp_id); emp_id:=emp_id+1; END LOOP
ITERATIVE CONTROL
FOR LOOP:
Is an optimized for loop for cursor so that a record variable need not be created. Syntax:
FOR ctrl_var IN start . . End LOOP Statements; END LOOP
Here, no need to declare ctrl_var. Inside loop, you cant assign value to ctrl_var, Its read only variable. Instead of start . . End, ypu can write SQL query.
Eg.
BEGIN FOR someone IN (SELECT * FROM employees WHERE employee_id < 120 ) LOOP
DBMS_OUTPUT.PUT_LINE('First name = ' || someone.first_name ||, Last name = ' || someone.last_name);
CURSOR Is a memory object. It creates a stream between session and oracle process i.e. database. It fetch the data row by row from table and passes it to session. When the last row is fetched ,implicitly the cursor is closed so that the memory occupied by cursor gets released. Types:
CURSOR
Implicit cursor:
Created when DML statement is fired i.e. INSERT, UPDATE, DELETE. Implicit cursor name is sql.
Explicit cursor:
Used to retrive multiple rows. Eg.
When u declare a cursor, the query isnt get fired. The query get fired when u fetch 1st row into record type.
THANK YOU