0% found this document useful (0 votes)
9 views

Pl-SQL Seminar New

The document provides an overview of PL/SQL, a procedural language extension to SQL, detailing its features such as blocks, variables, program flow control, cursors, functions, procedures, error handling, packages, triggers, and jobs. It emphasizes the integration of PL/SQL with the Oracle database server and highlights its capabilities in data manipulation and modular programming. Additionally, it covers control structures, dynamic PL/SQL, and the advantages of using PL/SQL in database applications.

Uploaded by

aadityashah1020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Pl-SQL Seminar New

The document provides an overview of PL/SQL, a procedural language extension to SQL, detailing its features such as blocks, variables, program flow control, cursors, functions, procedures, error handling, packages, triggers, and jobs. It emphasizes the integration of PL/SQL with the Oracle database server and highlights its capabilities in data manipulation and modular programming. Additionally, it covers control structures, dynamic PL/SQL, and the advantages of using PL/SQL in database applications.

Uploaded by

aadityashah1020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

PL/SQL

Procedural Language / Structured Query


Language

v
Overview
 Overview of PL/SQL
 Blocks
 Variables and placeholders
 Program Flow Control Statements
 Cursors
 Functions and Procedures
 Error Handling
 Packages
 Triggers
 Jobs

1 June 2006 2
PL/SQL
 Procedural language extension to SQL
 procedural data manipulation
 conditionals, loops etc.
 High-level language features
 Complex data types
 Data encapsulation
 Modular programming
 Integrated with the ORACLE database server
 Server-side
 parsing / compilation
 execution / interpretation
 End-user platform independent (like SQL)
1 June 2006 3
PL/SQL execution

1 June 2006 4
PL/SQL placeholders
 All SQL types are supported by PL/SQL
 Numerical types
 NUMBER, PLS_INTEGER
 Many derived types, e.g. POSITIVE
 Character types
 CHAR, VARCHAR2, NCHAR,…
 Other scalar types
 BOOLEAN, DATE, UROWID, RAW

1 June 2006 5
PL/SQL placeholders
 Scalar type
 variable
 constant

 Composite/vector type
 record
 used for reading rows from table

 Collections
 Associative Array - dictionary
 Variable-sized Array (VARRAY) – fixed size
 Nested Tables – dynamic size

1 June 2006 6
PL/SQL placeholders
 Scalar type
 variable
 constant

DECLARE
l_x NUMBER := 20000;
l_message VARCHAR2(40);
C_PI CONSTANT NUMBER(3,2):=3.14;
BEGIN
l_x := 1000 * C_PI;
l_message := 'Hello world';
END;

1 June 2006 7
PL/SQL placeholders
 Scalar type
DECLARE
 variable
TYPE T_POPULATION IS TABLE OF NUMBER INDEX BY VARCHAR2(64);
 constant
l_city_population T_POPULATION;
l_i number;
BEGIN
 Single composite/vector type
l_city_population('Smallville') := 2000;
l_i:= l_city_population('Smallville') ;
 record
END; 
used for reading rows from table
/

 Collections
 Associative Array
 Variable-sized Array (VARRAY)
 Nested Tables

1 June 2006 8
PL/SQL placeholders

DECLAREScalar type
TYPE T_FOURSOME IS VARRAY(4) OF VARCHAR2(15);
 variable
l_team T_FOURSOME := T_FOURSOME('John', 'Mary', 'Alberto');
BEGIN  constant
l_team.EXTEND; -- Append one null element
l_team(4):='Mike'; -- Set 5th element element
Single composite/vector type
DBMS_OUTPUT.PUT_LINE( l_team( l_team.first ) ); -- Print first element
DBMS_OUTPUT.PUT_LINE( l_team( l_team.last ) ); -- Print last element
 record

END;  used for reading rows from table


/

 Collections
 Associative Array
 Variable-sized Array (VARRAY)
 Nested Tables

1 June 2006 9
PL/SQL placeholders
 Scalar type
DECLARE
 variable
TYPE T_ROSTER IS TABLE OF VARCHAR2(15);
l_names T_ROSTER := T_ROSTER('D Caruso', 'J Hamil', 'D Piro', 'R Singh');
 constant
l_i number;
BEGIN
FOR l_i IN l_names.FIRST .. L_names.LAST LOOP --For first to last element
 Single composite/vector type
DBMS_OUTPUT.PUT_LINE(l_names(l_i));
record
END LOOP;
END;
 used for reading rows from table
/

 Collections
 Associative Array
 Variable-sized Array (VARRAY)
 Nested Tables

1 June 2006 10
PL/SQL Control Structures
 Conditional Control
 Using IF and CASE statements

DECLARE
DECLARE := 20000;
l_sales NUMBER(8,2)
l_grade CHAR(1) := 'B';
l_bonus NUMBER(6,2);
BEGIN BEGIN
CASETHEN
IF l_sales > 50000 l_grade
l_bonus := 1500;
WHEN l_bonus
ELSIF l_sales > 35000 THEN 'A' THEN
:= 500;
DBMS_OUTPUT.PUT_LINE('Excellent');
ELSE l_bonus := 100;
END IF; WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('Very
Good');
UPDATE employees SET salary = salary + l_bonus;
WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('Good');
END;
WHEN 'D' THEN DBMS_OUTPUT.PUT_LINE('Fair');
WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('Poor');
ELSE DBMS_OUTPUT.PUT_LINE('No such grade');
 Sequential Control END CASE;
END;
 Using GOTO statement

1 June 2006 11
PL/SQL Control Structures
 Iterative loops DECLARE
l_i NUMBER := 0;
 Simple loop (infinite) BEGIN
LOOP
 WHILE loop DBMS_OUTPUT.PUT_LINE (TO_CHAR(l_i));
l_i:=l_i+1;
 FOR loop END LOOP;

 Numeric range WHILE l_i < 10 LOOP


 Reversed DBMS_OUTPUT.PUT_LINE (TO_CHAR(l_i));
l_i := l_i + 1;
 Cursor based END LOOP;

FOR l_i IN 1..500 LOOP


DBMS_OUTPUT.PUT_LINE (TO_CHAR(l_i));
END LOOP;

FOR l_i IN REVERSE 1..3 LOOP


DBMS_OUTPUT.PUT_LINE (TO_CHAR(l_i));
END LOOP;
END;

1 June 2006 12
PL/SQL Control Structures
 Iterative loops DECLARE
l_i NUMBER := 0;
 Named loops l_j NUMBER := 0;
l_s NUMBER :=0;
BEGIN
<<outer_loop>>
 Exiting loops LOOP
l_i := l_i + 1;
 EXIT statement <<inner_loop>>
LOOP
 Loop skipping l_j := l_j + 1;
l_s := l_s + l_i * l_j;
 CONTINUE EXIT inner_loop WHEN (l_j >
5);
EXIT outer_loop WHEN ((l_i
* l_j) > 15);
END LOOP inner_loop;
DBMS_OUTPUT.PUT_LINE('Sum:'||
TO_CHAR(l_s));
IF l_s > 100 THEN EXIT;
END IF;
END LOOP outer_loop;
1 June 2006 END; 13
Dynamic PL/SQL
 Execution of statement composed in
strings
 For SQL which text is unknown at
compiling time
 Some parts of SQL cannot be bind by
variables
 tablename
 database link
…

 Be aware of SQL injections!


 Use dynamic SQL when it is really
1 June 2006 14
Dynamic SQL & PL/SQL
 Inserting
sql_stmt := 'INSERT INTO payroll VALUES (:x, :x, :y, :x)';
EXECUTE IMMEDIATE sql_stmt USING a, b; -- using variables

 Selecting data from dynamic table_name


EXECUTE IMMEDIATE 'select id form '||table_name||' where name=:a '
using job_name returning into job_id;

 Dynamic PL/SQL
plsql_block := 'BEGIN calc_stats(:x, :x, :y, :x); END;';
EXECUTE IMMEDIATE plsql_block USING a, b;

1 June 2006 15
PL/SQL Procedures

 Procedure definition

CREATE OR REPLACE PROCEDURE EXE$RAISE_SALARY (p_emp_id IN NUMBER


, p_amount IN NUMBER) IS
BEGIN
UPDATE employees SET salary = salary + p_amount
WHERE employee_id = p_emp_id;
END EXE$RAISE_SALARY;

 Procedure invocation
EXE$RAISE_SALARY(emp_num, bonus);
EXE$RAISE_SALARY(l_amount => bonus, l_emp_id => emp_num);
EXE$RAISE_SALARY(emp_num, l_amount => bonus);

1 June 2006 16
PL/SQL Functions
 Function definition
CREATE OR REPLACE FUNCTION STF$HALF_OF_SQUARE (p_original NUMBER)
RETURN NUMBER IS
BEGIN
RETURN (p_original * p_original)/2 + (p_original * 4);
END STF$HALF_OF_SQUARE;

 Function invocation
square INTEGER := STF$HALF_OF_SQUARE(25);

select STF$HALF_OF_SQUARE( a ) from squers;

1 June 2006 17
Error Handling
 An error interrupts the execution of the
program
 An exception is raised

 Exception to be handled
 in the exception section or
 will be propagated to the enclosing block
 After the exception is handled, the control
passes to the enclosing block

1 June 2006 18
PL/SQL Exceptions
 The programmer can create, name and raise
exception
 Exceptions can by caught and handled by the
user’s code
 Exceptions does not rollback or commit
changes!
 Categories
 Internally defined (without name, just error code)
 Predefined (with name and error code)
 User-defined (with name, raised always
explicitly)
1 June 2006 19
PL/SQL Exceptions

DECLARE
l_out_of_stock EXCEPTION;
l_number_on_hand NUMBER := 0;
BEGIN
IF l_number_on_hand < 1 THEN
RAISE l_out_of_stock;
END IF;
EXCEPTION
WHEN l_out_of_stock THEN
DBMS_OUTPUT.PUT_LINE ( 'Encountered out of stock error' );
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ( 'Houston we''ve got a problem!' );
END;
END; Oracle Tutorials: PL/SQL
1 June 2006 20
Packages
 Group logically related PL/SQL types,
items and modules
 2 parts:
 Specification  public interface
 Body  private implementation
 Packages are global
 Cannot be called, parameterized, or nested.
 Package state persist for the duration of the
database session

1 June 2006 21
Why use Packages
 Modularity
 Encapsulation of data and functionality
 Clear specifications independent of the
implementation
 Easier development
 Added functionality:
 global variables
 global types
 Better performance
1 June 2006 22
Package Specification

 Header
 Declarations of global types and
variables
 Specification of cursors
 With RETURN clause, but no SELECT statement
 Specification of public modules

1 June 2006 23
Advantages of PL/SQL
 Tightly integrated with SQL
 Reduced network traffic
 Portability - easy deployment and
distribution
 Data layer separated from client
language
 Modification without changing of
application code
 Can be shared by many platform
 Server-side periodical data maintenance
(jobs)
1 June 2006 24

You might also like