PL/SQL
PROCEDURAL LANGUAGE/STRUCTURE QUERY LANGUAGE
FEATURES OF PL/SQL
BLOCK STRUCTURE LAGUAGE DECLARING VARIABLES AND CONSTANTS DECLARING ATTRIBUTES CONTROL STRUCTURES CURSORS EXCEPTION HANDLING SUB PROGRAMS DATA ENCAPSULATION
ADVANTAGES OF PL/SQL
SUPPORT FOR OOP SUPPORT FOR SQL INTEGRATION WITH ORACLE MODULARITY BETTER PERFORAMANCE PORTABILITY
BLOC STRUCTURE LANGUAGE
SET OF EXECUTABLE STATEMENTS STATEMENTS ARE EITHER PL OR SQL STATEMENTS
STATEMENTS
THERE ARE TWO TYPES OF STATEMENTS
NAMED PL/SQL BLOC UNNAMED PL/SQL BLOC
PL/SQL BLOC
ITS MADE OF 3 SECTIONS
DECLARATIVE SECTION EXECUTABLE SECTION EXCEPTION HANDLING SECTION
DECLARATIVE BLOC
ITS MEANT FOR DECLARING VARIABLESAND CONSTANTS
THIS SECTION CAN BE OPTIONAL
EXECUTABLE STATEMENTS SECTION
MEANT FOR MANIPULATING OBJECTS DURING EXECUTION
THIS IS A REQURIED SECTION
EXCEPTION HANDLING SECTION
ANY ERRORS OR EXCEPTIONS ARE RAISED DURING THE EXECUTION OF THE PL/SQL BLOCK ARE HANDLED HERE.
THIS SECTION IS OPTIONAL.
PL/SQL BLOC
DECLARE declarations; BEGIN executable statements; EXCEPTION exception handlers; End;
PL/SQL Engine in RDBMS
ORACLE RDBMS
PL/SQL ENGINE
DECLARE PROCEDURAL BEGIN PRODEDURAL SQL
Procedural Statement Executor
END;
SQL Statement Executor
Declaring variables and constants
Variables user defined data types of SQL <VARIABLENAME><DATATYPE> [SIZE][:=,<VALUE>]; Supports Binary Integers,Boolean values Support for all of the data types of SQL
Examples
A Number(6); S Varchar2(10);
J Varchar2(10):=xyz; B Number(6):=100;
Assigning values to variables
A:=100; S:=abc; Eg: Select ename into s from emp where empno=7900; Select ename,sal into S,A from emp where empno=7900;
Constants
<variable>constant<datatype>:=<value>; PI constant Number(4,2):=3.142;
Simple pl/sql Example
declare eno number(6):=&eno; n varchar2(10); s number(5); begin select ename,sal into n,s from emp where empno=eno; dbms_output.put_line('hello'); dbms_output.put_line('employeename'||n); dbms_output.put_line('employeesal'||s); end; /
Steps to execute
Ed filename.sql Set serveroutput on @ filename.sql
Lexical unit
It is a collection of all the parts, group of executablecharacters,identifiers,literals,del imiters,comments of a pl/sql bloc. Eg: Assignment operators or compound statement
TOTAL=SAL*12;----- COMMENT
Identifiers Simple (symbol) comment
Numerical value
Write a Pl/SQL bloc to insert values in to a Dept table.
declare dno number(6):=&dno; N varchar2(10):='&dname'; L varchar2(15):='&Location'; begin insert into dept values(Dno,N,L); dbms_output.put_line('NEW ROW CREATED'||n); end; /
Enter value for dno: 40 old 2: dno number(6):=&dno; new 2: dno number(6):=40; Enter value for dname: textile old 3: N varchar2(10):='&dname'; new 3: N varchar2(10):='textile'; Enter value for location: hyd old 4: L varchar2(15):='&Location'; new 4: L varchar2(15):='hyd'; NEW ROW CREATEDtextile PL/SQL procedure successfully completed.
Attributes
Variables can be declared with out the knowledge of data type Table attention will not effect the program
Eg. <VariableName><TableName><ColumnNam e><%TYPE> There are two types of attributes
%TYPE %ROWTYPE
%TYPE
ex A EMP.EMPNO%TYPE; S EMP.JOB%TYPE;
%ROWTYPE To store the entire row of tables <variable><Tablename>%RowType;
ex: R Emp%ROWTYPE;
example
ed row.sql declare eno emp.empno%type:=&empno; R emp%ROWTYPE; begin select * into r from emp where empno=eno; dbms_output.put_line(R.empno||R.ename||R.sal||R.deptno); end; >set serveroutput on; >@ row.sql Enter value for empno: 7902 old 2: eno emp.empno%type:=&empno; new 2: eno emp.empno%type:=7902; 7902FORD300020 PL/SQL procedure successfully completed.
Control Statements
conditional control iterative control sequential control
Conditional control