Oracle Database 11G: PL/SQL Programming
Oracle Database 11G: PL/SQL Programming
PL/SQL Programming
Association:
: & % => . @
Concatenation:
||
Comparison:
= - <> != ^= > < >= <=
Delimiter:
‘ ( ) , << >> -- /* */ “
Math:
+ - * / **
Statement:
;
Character and Lexical Units
Identifiers:
Reserved words and keywords:
names built in Oracle
SELECT DECLARE NOT BEGIN …
(Appendix I)
Predefined identifier:
names defined in STANDARD package
SYSDATE TO_CHAR SQLCODE …
(Appendix J)
Quoted identifier:
use quote you can then use reserved word. Avoid it.
“END” number := 1;
Named block:
CREATE OR REPLACE <function name>|<procedure name>
…
IS
…
BEGIN
…
[EXCEPTION]
…
END;
/
Variable Types
Scalar Datatypes:
BOOLEAN:
var1 boolean := true;
CHAR:
city char(2) := ‘NY’;
VARCHAR2:
str varchar2(32767) := ‘bla bla bla’;
DATE:
dt date := sysdate;
NCHAR:
NVARCHAR2:
NUMBER:
n1 number := 123;
n2 number(5,2) := 123.45;
BINARY_INTEGER, PLS_INTEGER,
BINARY_FLOAT, BINARY_DOUBLE
Variable Types
Large Objects (LOBs):
BFILE:
file1 BFILE := bfilename('my_media_dir','king.avi');
dbms_lob.fileopen(file1);
BLOB:
b1 BLOB := empty_blob();
dbms_lob.loadfromfile (b1, …);
CLOB, NCLOB:
c1 CLOB := empty_clob();
dbms_lob.writeappend (c1, 32000, ‘a long article’);
Variable Types
Composite Datatypes:
• Records
DECLARE
type rec_tp is record (eid number, ename varchar2(20));
v_rec rec_tp;
BEGIN
v_rec.eid := 101;
v_rec.ename := ‘John’;
dbms_output.put_line(v_rec.eid || ‘: ‘ || v_rec.ename);
END;
/
Variable Types
Composite Datatypes:
• Varray
DECLARE
type number_varray is varray(7) of number;
list number_varray := number_varray(1,2,3, null, 5, null, null);
BEGIN
for I in 1..list.limit loop
dbms_output.put( list(i) || ' | ');
end loop;
dbms_output.new_line;
END;
/
Variable Types
Composite Datatypes:
• Nested Table
DECLARE
type number_table is table of number;
list number_table := number_table(1,2,3,4,5);
BEGIN
list.delete(2);
for i in 1..list.count loop
if list.exists(i) then
dbms_output.put( i || '-' || list(i) || ' | ');
end if;
end loop;
dbms_output.new_line;
END;
/
Variable Types
Composite Datatypes:
DECLARE
type number_table is table of number index by pls_integer;
list number_table;
BEGIN
for i in 1..6 loop
list(i*10) := i;
end loop;
list.delete(20);
for i in 1..list.count loop
if list.exists(i*10) then
dbms_output.put( i*10 || '-' || list(i*10) || ' | ');
end if;
end loop;
dbms_output.new_line;
END;
/
Variable Types
System Reference Cursors:
DECLARE
father_name VARCHAR2(20):='Patrick';
date_of_birth DATE:='20-Apr-1972';
BEGIN
DECLARE
child_name VARCHAR2(20):='Mike';
date_of_birth DATE:='12-Dec-2002';
BEGIN
DBMS_OUTPUT.PUT_LINE('Father''s Name: '||father_name);
DBMS_OUTPUT.PUT_LINE('Date of Birth: '||date_of_birth);
DBMS_OUTPUT.PUT_LINE('Child''s Name: '||child_name);
END;
DBMS_OUTPUT.PUT_LINE('Date of Birth: '||date_of_birth);
END;
/
END
chap 2-14