Professional Documents
Culture Documents
PL/SQL
• Character set
• Operators
• Literals
• Variables and constants
• Data types
• Declarations
• Assignments
• Comments
Declarations
Before a variable can be used in the begin section, it must be
declared in the declare section. The syntax for declaring a
variable is as follows:
Syntax:
Variable-name datatype (size);
Examples:
Age Number(5);
A Number(10,2);
Name Varchar2(20);
DOB Date;
Declarations
Num := #
%TYPE AND %ROWTYPE
%ROWTYPE
%ROWTYPE
dept_rec dept%ROWTYPE;
Now to refer the fields, dot notation can be used
as shown in the following example:
dept_rec.deptno;
dept_rec.deptname;
DISPLAYING USER MESSAGE
ON THE SCREEN
Syntax:
SQL> SET SERVEROUTPUT ON;
DISPLAYING USER MESSAGE
ON THE SCREEN
• Now we can display any message using
DBMS_OUTPUT.PUT_LINE command as follows:
DBMS_OUTPUT.PUT_LINE(‘Well Come To Computer Lab’);
• To display value of a variable A, following command can be
used:
DBMS_OUTPUT.PUT_LINE (A);
To display value of a variable A along with some message,
following command can be used:
Begin
dbms_output.put_line ('Hello');
End;
SOME BASIC PL/SQL
PROGRAMS
• Write a PL/SQL code block to add two
numbers and display the addition.
Declare
a number(2);
b number(2);
c number(2);
Begin
a:=5;
b:=4;
c:=a + b;
dbms_output.put_line ('sum='||c);
End;
SOME BASIC PL/SQL
PROGRAMS
Declare
a number(5);
b number(5);
t number(6);
Begin
Select ta, da into a, b from emp where empid = 8;
t := a + b;
Update emp set total = t where empid = 8;
End;
SOME BASIC PL/SQL
PROGRAMS
Declare
a emp.ta%type;
b emp.da%type;
t emp.total%type;
Begin
Select ta, da into a, b from emp where empid = 8;
t := a + b;
Update emp set total = t where empid = 8;
End;
SOME BASIC PL/SQL
PROGRAMS
Declare
Record emp%rowtype;
Begin
Select*into Record from emp where empid = 8;
Record.total := Record.ta + Record.da;
Update emp set total=Record.total where empid = 8;
End;
CONTROL STATEMENTS
• Conditional control
• Iterative control
• Sequence control
Conditional Control
• Syntax:
LOOP
Sequence-of-statements;
EXIT WHEN condition;
END LOOP;
LOOP
• Write a program to display the numbers
from 1 to 10.
Declare
i number(2);
Begin
i:=1;
LOOP
dbms_output.put_line(i);
i:=i + 1;
EXIT WHEN i > 10;
END LOOP;
End;
WHILE-LOOP
Syntax:
• Syntax:
• Syntax:
GOTO label-name;
Sequence Control
Declare
num1 number(2);
num2 number(2);
Begin
num1:=&num1;
num2:=&num2;
IF num1 > num2 THEN
GOTO O1;
ELSE
GOTO O2;
END IF;
<<O1>>
dbms_output.put_line('greater is num1='||num1);
GOTO O3;
<<O2>>
dbms_output.put_line('greater is num2='||num2);
<<O3>>
dbms_output.put_line('successful');
End;
Triggers
Database trigger is the stored procedure that is
automatically executed in response to certain events on
a particular table or view in a database. An event on a
table can be INSERT, DELETE or UPDATE statement
on that table
Triggers Procedures
•SQL%ISOPEN
•SQL%FOUND
•SQL%NOTFOUND
•SQL%ROWCOUNT
•SQL%ISOPEN is always FALSE because the Oracle engine closes the
implicit cursor automatically after executing the SQL statement. The meaning of
other three attributes is same as that of general cursor attributes as mentioned
above in the attribute table.
Cursors
• Explicit Cursor
• %ISOPEN
• %FOUND
• %NOTFOUND
• %ROWCOUNT
Cursors
Steps in Handling Explicit Cursor
•Syntax:
Cursor CursorName IS SELECT statement;
•Here, SELECT statement can use all its clauses except INTO clause.
•Example:
• Opening a cursor executes the query and identifies the result set,
which consists of all rows that meet the query search criteria. The
syntax for opening a cursor is as follows:
•
• Syntax:
Open CursorName;
• Example:
Open C;
Cursors
• Fetching a Record from Cursor
After opening a cursor, FETCH statement is used to load the rows from
the result set into the memory variables but, one row at a time. After
each fetch, the cursor advances to the next row in the result set.
The syntax for FETCH is as follows:
• Syntax:
• FETCH CursorName INTO Variables;
• For each column value returned by the query associated with the
cursor, there must be a corresponding, type-compatible variable in
the INTO list.
• Example:
FETCH C INTO my_rollno, my_name;