Professional Documents
Culture Documents
PL/SQL P L SQL Prepared By: Manoj Kathpalia Edited By: M V Ramakrishna
PL/SQL P L SQL Prepared By: Manoj Kathpalia Edited By: M V Ramakrishna
BEGIN
(Executable section)
EXCEPTION
(Exception handling section)
END;
/ (/ at the end of a block tells Oracle to run the block)
Types of PL/SQL Blocks
• as Anonymous blocks which are
compiled and run when loaded.
• as Triggers to maintain integrity.
• as Subprograms (Procedures and
Functions) stored within the database
that can be executed many times.
• as Packages, a named declarative
section for storing related objects that
can include procedures, functions and
variables.
PL/SQL Syntax
Declaration
• Variables are declared in the
declarative section of the block.
• Variable declaration examples:
v_student_id CHAR(8);
v_lastname VARCHAR2(25);
v_capacity NUMBER(3) := 200;
( := is used to initialize variables )
Types
Basic PL/SQL types are:
• NUMBER(P,S) holds numeric value
(where P is the precision and S is the scale)
LOOP
sequence_of_statements;
END LOOP:
Comments
• Single-line comments
DECLARE
v_title CHAR(2) -- title can be Mr or Ms
…
• Multi-line comments
DECLARE
/* I am a multi-line
comment */
• -- Command to display the output at the SQL prompt
• SET SERVEROUTPUT ON
• -- This PL/SQL block accepts a number from the terminal, and prints out
its square.
• -- You can try this from sql+
• -- It has no exception handling code.
• DECLARE
• num1 NUMBER(3);
• num1_sq NUMBER(6);
• BEGIN
• -- prompts user for a value for 'anytemp'
• num1 := &anytemp;
• num1_sq := num1 * num1 ;
• DBMS_OUTPUT.PUT_LINE('THE SQUARE OF ' || num1 || ' IS ' ||
num1_sq);
• END;
• /