Sample Table: USER: ID Name AGE Address Salary
Sample Table: USER: ID Name AGE Address Salary
Sample Table: USER: ID Name AGE Address Salary
1. What is PL/SQL?
Pl/SQL stands for "Procedural Language extension of SQL" that is used in Oracle.
PL SQL is a procedural language which has interactive SQL, as well as procedural
programming language constructs like conditional branching and iteration.
PL/SQL is not case sensitive so you are free to use lower case letters or upper case
letters except within string and character literals.
What it comprises Data source for reports, web Application language to build, format and
pages display report, web pages
14.What are the two virtual tables available at the time of database trigger
execution?
Table columns are referred as THEN.column_name and NOW.column_name.
For INSERT related triggers, NOW.column_name values are available only.
For DELETE related triggers, THEN.column_name values are available only.
For UPDATE related triggers, both Table columns are available.
To execute this trigger we need to perform operation on Table using procedure: Check the
salary difference by procedure:
1. DECLARE Output:
2. total_rows number(2);
Old salary: 20000
3. BEGIN
New salary: 25000
4. UPDATE customers Salary difference: 5000
5. SET salary = salary + 5000; Old salary: 22000
6. IF sql%notfound THEN New salary: 27000
7. dbms_output.put_line('no customers updated'); Salary difference: 5000
Old salary: 24000
8. ELSIF sql%found THEN
New salary: 29000
9. total_rows := sql%rowcount; Salary difference: 5000
10. dbms_output.put_line( total_rows || ' customers updated ');
11. END IF;
12. END; /
Note: As many times you executed this code, the old and new both salary is incremented by
5000 and hence the salary difference is always 5000.
Because we are performing update operation on Salary in table and trigger is set on Salary field.
1. BEGIN Output:
2. insertuser(101,'Rahul'); ID, Name
101, Rahul
3. dbms_output.put_line('record inserted successfully');
record inserted successfully
4. END;
5. /
1. CREATE [OR REPLACE] FUNCTION function_name [parame1. create or replace function adder(n1 in number, n2 i
ters] n number)
2. [(parameter_name [IN | OUT | IN OUT] type [, ...])] 2. return number
3. RETURN return_datatype 3. is
4. {IS | AS} 4. n3 number(8);
5. BEGIN 5. begin
6. < function_body > 6. n3 :=n1+n2;
7. END [function_name]; 7. return n3;
8. end;
9. /
Now write another program to execute this function:
1. DECLARE Output:
2. n3 number(2); Addition is: 33
Statement processed.
3. BEGIN
0.05 seconds
4. n3 := adder(11,22);
5. dbms_output.put_line('Addition is: ' || n3);
6. END;
7. /
Another Example:
1. DECLARE Output:
2. a number;
Maximum of (23,45): 45
3. b number;
Statement processed.
4. c number; 0.02 seconds
5. FUNCTION findMax(x IN number, y IN number)
6. RETURN number
7. IS
8. z number;
9. BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;
/
Recursive Call
1. DECLARE Output:
2. num number;
Factorial 6 is 720
3. factorial number;
PL/SQL procedure successfully completed.
4.
5. FUNCTION fact(x number)
6. RETURN number
7. IS
f number;
BEGIN
IF x=0 THEN
f := 1;
ELSE
f := x * fact(x-1); //Recursive call to self
END IF;
RETURN f;
END;
BEGIN
num:= 6;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' || factorial);
END;
/
Implicit Cursors
Explicit Cursors
The implicit cursors are automatically generated by Oracle while an SQL statement is executed,
if you don’t use an explicit cursor for the statement.
These are created by default to process the statements when DML statements like INSERT,
UPDATE, DELETE etc. are executed.
Oracle provides some attributes known as Implicit cursors attributes to check the status of DML
operations. Some of them are: %FOUND, %NOTFOUND, %ROWCOUNT and %ISOPEN.
When you execute the SQL statements like INSERT, UPDATE, DELETE then the cursor attributes
tell whether any rows are affected and how many have been affected.
Example Output
1. DECLARE
6 customers updated
2. total_rows number(2);
PL/SQL procedure successfully
3. BEGIN completed.
4. UPDATE customers
5. SET salary = salary + 5000;
6. IF sql%notfound THEN
7. dbms_output.put_line('no customers updated');
8. ELSIF sql%found THEN
9. total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers updated ');
END IF;
END;
/
25.What are PL/SQL Explicit Cursors?
The Explicit cursors are defined by the programmers to gain more control over the context
area. These cursors should be defined in the declaration section of the PL/SQL block. It is
created on a SELECT statement which returns more than one row.
Example Output
1. DECLARE
1 Ramesh Allahabad
2. c_id customers.id%type;
2 Suresh Kanpur
3. c_name customers.name%type; 3 Mahesh Ghaziabad
4. c_addr customers.address%type; 4 Chandan Noida
5. CURSOR c_customers is 5 Alex Paris
6. SELECT id, name, address FROM customers; 6 Sunita Delhi
7. BEGIN PL/SQL procedure successfully
completed.
8. OPEN c_customers;
9. 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;
/
Steps:
You must follow these steps while working with an explicit cursor.
1. Declare the cursor to initialize in the memory.
2. Open the cursor to allocate memory.
3. Fetch the cursor to retrieve data.
4. Close the cursor to release allocated memory.
%FOUND : Checks if the cursor has fetched any row. It is true if rows are fetched
%NOT FOUND : Checks if the cursor has fetched any row. It is True if rows are not fetched.
System-defined Exceptions
User-defined Exceptions
31.Example of System Exceptions?
Example Output
1. DECLARE No such customer!
2. c_id customers.id%type := 8; PL/SQL procedure successfully
completed.
3. c_name customers.name%type;
4. c_addr customers.address%type; There is no customer with ID value 8 in
5. BEGIN database, so the program raises the run-
6. SELECT name, address INTO c_name, c_addr time exception NO_DATA_FOUND,
7. FROM customers which is captured in EXCEPTION block.
8. WHERE id = c_id;
9. DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
LOGON_DENIED, VALUE_ERROR
1. DECLARE
2. exception_name EXCEPTION;
3. BEGIN
4. IF condition THEN
5. RAISE exception_name;
6. END IF;
7. EXCEPTION
8. WHEN exception_name THEN
9. statement;
10. END;
PL/SQL facilitates their users to define their own exceptions according to the need of the
program. A user-defined exception can be raised explicitly, using either a RAISE statement or
the procedure DBMS_STANDARD.RAISE_APPLICATION_ERROR.
1. DECLARE
2. my-exception EXCEPTION;
JULIAN