RDM Chapter-4
RDM Chapter-4
Chapter-4
PL/SQL Programming
Prepared By
Ms. H C. Kunwar
GHRCEM, Nagpur
What is PL/SQL :
SQL stands for Structured Query Language
SQL allows you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views
1. Portability.
2. Modularized program development
3. High Performance, High Productivity, Scalability, Manageability.
4. Support for Object-Oriented Programming.
5. Support for Developing Web Applications
6. It improves performance against running SQL queries multiple times
7. It provides exception handling.
8. It provides built-in libraries & packages.
9. It integrates procedural constructs with SQL.
DECLARE
Variable declaration
BEGIN
Program Execution
EXCEPTION
Exception handling
END;
RAISONI GROUP OF INSTITUTIONS
PL/SQL Data Types
Datatype of Pl/SQL
Datatype Description
Declare
A number(3);
B number(3);
Begin
A:=&A;
B:=&B;
If (A>B) then
Dbms_output.put_line(‟ Greater number=‟||A);
else
Dbms_output.put_line(‟ Greater number=‟||B);
End if;
End;
Syntax:
WHILE condition LOOP
sequence_of_statements;
END LOOP;
Example:
i number := 1;
while (i<=10) Loop
dbms_output.put_line(i);
i :=i+1;
End Loop
Syntax:
Example:
Program :
DECLARE
num number:=#
fact number:=1;
i number;
BEGIN
for i in 1..num loop
fact:=fact*i;
end loop;
Program :
declare
n number:=&n;
begin
if mod(n,2)=0 then
dbms_output.put_line('number is even');
else
dbms_output.put_line('number is odd');
end if;
end;
/
Q3. Write a PL/SQL CODE USING WHILE LOOP DISPLAY N EVEN NUMBERS.
Exception Handling : Exception handling is nothing but a code block in memory that
will attempt to resolve current exception condition.
Syntax:
DECLARE
Declaration section
…executable statement;
EXCEPTION
WHEN ex_name1 THEN
-Error handling statements/user defined action to be carried out;
END;
It must be declare by the user in the declaration part of the block where the exception is
used.
It is raised explicitly in sequence of statements using:
Raise_application_error(errorno,errorname);
Cursor :
A cursor is a temporary work area created in the system memory when a SQL
statement is executed.
A work area used for internal processing in order to execute SQL statement is called
as a cursor.
In other words, a cursor can hold more than one row, but can process only one row at
a time. The set of rows the cursor holds is called the active set.
A cursor is a pointer to this context area. PL/SQL controls the context area through a
cursor.
A cursor holds the rows (one or more) returned by a SQL statement.
Each cursor contains the followings 4 steps,
1. Declare Cursor: In this part we declare variables and return a set of values.
2. Open: This is the entering part of the cursor.
3. Fetch: Used to retrieve the data row by row from a cursor.
4. Close: This is an exit part of the cursor and used to close a cursor
• For INSERT operations, the cursor holds the data that needs to be inserted.
• For UPDATE and DELETE operations, the cursor identifies the rows that would be
affected.
• In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor,
which always has the attributes like %FOUND, %ISOPEN, %NOTFOUND, and
%ROWCOUNT.
The following table provides the description of the most used attributes
Procedure : It is named PL/SQL block which performs one or more specific task.
Syntax :-
CREATE OR REPLACE PROCEDURE procedure_name
[(parameter_name[IN | OUT | IN OUT] type […])] {IS | AS}
BEGIN
<PROCEDURE_BODY>
END procedure_name;
Function : A function is a logically grouped set of SQL and PL/SQL statements that perform a
specific task.. A function is same as a procedure except that it returns a value. A
function is created using the CREATE FUNCTION statement
Syntax:
CREATE OR REPLACE FUNCTION function_name(argument IN data type,…)
RETURN data type{ IS, AS}
Variable declarations;
Constant declarations;
BEGIN
PL/SQL subprogram body;
EXCEPTION
Exception pl/sql block;
END;
Example:
CREATE OR REPLACE FUNCTION Success_cnt
RETURN number
IS cnt number(7) := 0;
BEGIN
SELECT count(*) into cnt
FROM candidate where result='Pass';
RETURN cnt;
END;
/
Trigger :
Trigger is a pl/sql block structure which is fired when a DML statements like Insert,
Delete, Update is executed on a database table.
OR