PL/SQL
Dr.K.R.PRASANNA KUMAR
CSD
PL/SQL
Benefits of Using PL/SQL
SQL | PL/SQL |
SQL is a single query that is used to perform DML and DDL operations. | PL/SQL is a block of codes that used to write the entire program blocks/ procedure/ function, etc. |
It is declarative, that defines what needs to be done, rather than how things need to be done. | PL/SQL is procedural that defines how the things needs to be done. |
Execute as a single statement. | Execute as a whole block. |
Mainly used to manipulate data. | Mainly used to create an application. |
Cannot contain PL/SQL code in it. | It is an extension of SQL, so it can contain SQL inside it. |
PL/SQL - Basic Syntax
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
Data Types
Variables
variable_name [CONSTANT] datatype [NOT NULL] := initial_value;
Examples:
Literal Type & Example |
Numeric Literals 050 78 -14 0 +32767 6.6667 0.0 -12.0 3.14159 +7800.00 6E5 1.0E-8 3.14159e0 -1E38 -9.5e-3 |
Character Literals 'A' '%' '9' ' ' 'z' '(' |
String Literals 'Hello, world!' 'Tutorials Point' '19-NOV-12' |
BOOLEAN Literals TRUE, FALSE, and NULL. |
Date and Time Literals DATE '1978-12-25'; TIMESTAMP '2012-10-29 12:01:01'; |
DECLARE
message varchar2(20):= 'CSD';
BEGIN
dbms_output.put_line(message);
END;
/
set serveroutput on;
Operator | Description | Example |
+ | Adds two operands | A + B will give 15 |
- | Subtracts second operand from the first | A - B will give 5 |
* | Multiplies both operands | A * B will give 50 |
/ | Divides numerator by de-numerator | A / B will give 2 |
** | Exponentiation operator, raises one operand to the power of other | A ** B will give 100000 |
| Description | Example |
= | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A = B) is not true. |
!= <> ~= | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true |
Operator | Description | Example |
LIKE | The LIKE operator compares a character, string, or CLOB value to a pattern and returns TRUE if the value matches the pattern and FALSE if it does not. | If 'Zara Ali' like 'Z% A_i' returns a Boolean true, whereas, 'Nuha Ali' like 'Z% A_i' returns a Boolean false. |
BETWEEN | The BETWEEN operator tests whether a value lies in a specified range. x BETWEEN a AND b means that x >= a and x <= b. | If x = 10 then, x between 5 and 20 returns true, x between 5 and 10 returns true, but x between 11 and 20 returns false. |
IN | The IN operator tests set membership. x IN (set) means that x is equal to any member of set. | If x = 'm' then, x in ('a', 'b', 'c') returns Boolean false but x in ('m', 'n', 'o') returns Boolean true. |
IS NULL | The IS NULL operator returns the BOOLEAN value TRUE if its operand is NULL or FALSE if it is not NULL. Comparisons involving NULL values always yield NULL. | If x = 'm', then 'x is null' returns Boolean false. |
Operator | Description | Examples |
and | Called the logical AND operator. If both the operands are true then condition becomes true. | (A and B) is false. |
or | Called the logical OR Operator. If any of the two operands is true then condition becomes true. | (A or B) is true. |
not | Called the logical NOT Operator. Used to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make it false. | not (A and B) is true. |
IF
IF condition THEN
S;
END IF;
IF - ELSE
IF condition THEN
S1;
ELSE
S2;
END IF;
IF(boolean_expression 1)THEN
S1; -- Executes when the boolean expression 1 is true
ELSIF( boolean_expression 2) THEN
S2; -- Executes when the boolean expression 2 is true
ELSIF( boolean_expression 3) THEN
S3; -- Executes when the boolean expression 3 is true
ELSE
S4; -- executes when the none of the above condition is true
END IF;
CASE
CASE selector
WHEN 'value1' THEN S1;
WHEN 'value2' THEN S2;
WHEN 'value3' THEN S3;
...
ELSE Sn; -- default case
END CASE;
LOOP
LOOP
Sequence of statements;
END LOOP;
WHILE condition LOOP
sequence_of_statements
END LOOP;
FOR counter IN initial_value .. final_value LOOP
sequence_of_statements;
END LOOP;
Context area
PL/SQL Cursors
Types of cursors
Implicit cursors
Implicit Cursors
Implicit Cursors
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END IF;
END;
/
Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over the context area.
CURSOR cursor_name IS select_statement;
Where,
Four steps in using an Explicit Cursor
Declaring the Cursor
Declaring the cursor defines the cursor with a name and the associated SELECT statement. For example −
CURSOR c_customers IS
SELECT id, name, address FROM customers;
OPEN c_customers;
FETCH c_customers INTO c_id, c_name, c_addr;
CLOSE c_customers;
DECLARE
variables;
records;
CURSOR cursor_name IS select_statement;
BEGIN
OPEN cursor_name;
LOOP
FETCH cursor_name INTO variables OR records;
EXIT WHEN cursor_name%NOTFOUND;
process the records;
END LOOP;
CLOSE cursor_name;
END;
DECLARE
c_id customers.id%type;
c_name customers.name%type;
c_addr customers.address%type;
CURSOR c_customers is
SELECT id, name, address FROM customers;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name, c_addr;
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
CLOSE c_customers;
END;
/
SET SERVEROUTPUT ON;
DECLARE
CURSOR d_cursor IS
SELECT * from Dept;
D_Record Dept % ROWTYPE;
BEGIN
OPEN d_cursor;
LOOP
FETCH d_cursor INTO D_Record;
EXIT WHEN d_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(D_Record.Dname || ' ' || D_Record.Loc);
END LOOP;
CLOSE d_cursor;
END;
/
Triggers
Benefits of Triggers
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;