PL/SQL: By: Sonal Pandey M.E. CSE Dept. NITTTR Chandigarh
PL/SQL: By: Sonal Pandey M.E. CSE Dept. NITTTR Chandigarh
By : Sonal Pandey
M.E. CSE Dept.
NITTTR Chandigarh
WHAT IS PL/SQL
• Procedural Language – SQL
• An extension to SQL with design features
of programming languages (procedural
and object oriented)
• PL/SQL and Java are both supported as
internal host languages within Oracle
products.
WHY PL/SQL
• Acts as host language for stored procedures
and triggers.
• Provides the ability to add middle tier
business logic to client/server applications.
• Provides Portability of code from
one environment to another
• Improves performance of multi-query
transactions.
• Provides error handling
PL/SQL BLOCK STRUCTURE
DECLARE (optional)
-variable declarations
BEGIN (required)
- SQL statements
- PL/SQL statements or sub-blocks
EXCEPTION (optional)
- actions to perform when errors occur
END;(required)
PL/SQL - DATA TYPES
• PLS_INTEGER
• BINARY_INTEGER
• BINARY_FLOAT
• BINARY_DOUBLE
• FLOAT
• INT
• REAL
PL/SQL CHARACTER DATA TYPES
• CHAR
• VARCHAR2
• RAW
• NCHAR
• LONG
• LONG RAW
• ROWID
PL/SQL DATETIME
• YEAR
• MONTH
• DAY
• HOUR
• MINUTE
PL/SQL Large Object (LOB) Data Types
• BLOB
• CLOB
PL/SQL VARIABLES
• CREATE [OR REPLACE] FUNCTION function_name [parameters]
• [(parameter_name [IN | OUT | IN OUT] type [, ...])]
• RETURN return_datatype
• {IS | AS}
• BEGIN
• < function_body >
• END [function_name];
• Function_name: specifies the name of the function.
• [OR REPLACE] option allows modifying an existing
function.
• The optional parameter list contains name, mode and
types of the parameters.
• IN represents that value will be passed from outside and
OUT represents that this parameter will be used to
return a value outside of the procedure.
PL/SQL FUNCTION EXAMPLE
• Let's see a simple example to create a Now write another program to call the
function. function
• create or replace function adder(n1 in number, n2 i
n number) DECLARE
• return number n3 number(2);
• is
BEGIN
n3 := adder(11,22);
• n3 number(8); dbms_output.put_line('Addition is: ' || n3);
• begin END;
• n3 :=n1+n2; /
• return n3;
Output:
• end;
• / Addition is: 33 Statement processed. 0.05
seconds
PL/SQL SAMPLE PROGRAM
(with user input)
Set serveroutput on
Accept p_price Prompt 'Enter the Price: '
DECLARE
v_inv_value number(8,2);
v_price number(8,2);
v_quantity number(8,0) := 400;
BEGIN
v_price := &p_price;
v_inv_value := v_price * v_quantity;
dbms_output.put_line('******');
dbms_output.put_line('price * quantity=');
dbms_output.put_line(v_inv_value);
END;
/
Note: PL/SQL not designed for user interface programming
PL/SQL
COMMENTS
DECLARE
v_salary number(9,2) := 40000;
BEGIN
/* this is a multi-line comment that
will be ignored by the pl/sql
interpreter */
v_salary := v_salary * 2; -- nice raise
END; -- end of program
CURSORS
• A cursor is a private set of records
• 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.
• There are two types of cursors −
• Implicit cursors
• Explicit cursors
IMPLICIT CURSORS