PL SQL Concepts
PL SQL Concepts
PL SQL Concepts
PL/SQL
Developed by Oracle
Corporation in the
late 1980s as Direct call can also be
procedural extension made from external
Procedural language for SQL and programming
Language/Structured the Oracle relational language calls to
Query Language. database. database.
Composite Data items that have internal components that can be accessed individually.
Large Object (LOB) Pointers to large objects that are stored separately from other data items, such
as text, graphic images, video clips, and sound waveforms.
PL/SQL Numeric Data Types and Subtypes
BINARY_DOUBLE
4 Double-precision IEEE 754-format floating-point number
NUMBER(prec, scale)
Fixed-point or floating-point number with absolute value in range
5 1E-130 to (but not including) 1.0E126. A NUMBER variable can also
represent 0
DEC(prec, scale)
6 ANSI specific fixed-point type with maximum precision of 38
decimal digits
DECIMAL(prec, scale)
7 IBM specific fixed-point type with maximum precision of 38 decimal
digits
NUMERIC(pre, secale)
8 Floating type with maximum precision of 38 decimal digits
DOUBLE PRECISION
ANSI specific floating-point type with maximum precision of 126 binary
9 digits (approximately 38 decimal digits)
FLOAT
ANSI and IBM specific floating-point type with maximum precision of
126 binary digits (approximately 38 decimal digits)
10
INT
11 ANSI specific integer type with maximum precision of 38 decimal digits
INTEGER
ANSI and IBM specific integer type with
12 maximum precision of 38 decimal digits
SMALLINT
ANSI and IBM specific integer type with
13 maximum precision of 38 decimal digits
REAL
Floating-point type with maximum precision of
14 63 binary digits (approximately 18 decimal
digits)
PL/SQL Character Data Types and Subtypes
CHAR
1 Fixed-length character string with maximum size of 32,767 bytes
VARCHAR2
2 Variable-length character string with maximum size of 32,767
bytes
RAW
Variable-length binary or byte string with maximum size of
3 32,767 bytes, not interpreted by PL/SQL
NCHAR
4 Fixed-length national character string with maximum size of
32,767 bytes
NVARCHAR2
Variable-length national character string with maximum size of 32,767
5 bytes
LONG
6 Variable-length character string with maximum size of 32,760 bytes
LONG RAW
Variable-length binary or byte string with maximum size of 32,760 bytes,
7 not interpreted by PL/SQL
ROWID
8
Physical row identifier, the address of a row in an ordinary table
UROWID
9 Universal row identifier (physical, logical, or foreign row identifier)
PL/SQL Datetime and Interval Types
HOUR 00 to 23 0 to 23
MINUTE 00 to 59 0 to 59
00 to 59.9(n), where 9(n) is the precision of time fractional seconds 0 to 59.9(n), where 9(n)
is the precision of
SECOND interval fractional
seconds
TIMEZONE_HOUR -12 to 14 (range accommodates daylight savings time changes) Not applicable
TIMEZONE_MINU 00 to 59 Not applicable
TE
TIMEZONE_REGIO Found in the dynamic performance view V$TIMEZONE_NAMES Not applicable
N
Found in the dynamic performance view V$TIMEZONE_NAMES
TIMEZONE_ABBR Not applicable
Block Structure
• SET SERVEROUT ON;
• DECLARE
• pe_ratio NUMBER(3,2);
• BEGIN
• DBMS_OUTPUT.PUT_LINE('DK');
• pe_ratio :=12/0;
• EXCEPTION
• WHEN OTHERS THEN
• DBMS_OUTPUT.PUT_LINE('exception');
• END;
The PL/SQL Comments
Function
Implicit cursors
Explicit cursors
Implicit Cursors
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END IF;
END;
Explicit Cursors
Allocates the memory for the cursor and makes it ready for fetching the rows
returned by the SQL statement
OPEN c_customers;
CURSOR c_customers is
SELECT id, name FROM customers;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name;
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name);
END LOOP;
CLOSE c_customers;
END;
SQL Exception
set serveroutput on;
DECLARE
result number(10);
BEGIN
result:= 10/0;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error : '||SQLERRM|| ' SQL Error code: '||SQLCODE);
END;
INSERT INTO CUSTOMER (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Dinesh', 32, 'Himachal', 2000.00 );
INSERT INTO CUSTOMER (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Mohit', 25, 'Delhi', 1500.00 );
INSERT INTO CUSTOMER (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Krish', 23, 'Kota', 2000.00 );
INSERT INTO CUSTOMER (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Ajay', 25, 'Mumbai', 6500.00 );
INSERT INTO CUSTOMER (ID,NAME,AGE,ADDRESS,SALARY) VALUES (5, 'Ronald', 27, 'Bhopal', 8500.00 );
INSERT INTO CUSTOMER (ID,NAME,AGE,ADDRESS,SALARY) VALUES (6, 'Komal', 22, 'MP', 4500.00 );
CUSTOMER TABLE