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

Oracle Database 11G: PL/SQL Programming

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) User defined variables, subroutines, and data types: this is where we create all the identifiers.

Uploaded by

knpanchal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Oracle Database 11G: PL/SQL Programming

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) User defined variables, subroutines, and data types: this is where we create all the identifiers.

Uploaded by

knpanchal
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Oracle Database 11G

PL/SQL Programming

Copyright © Prof. Jack Chao. All rights reserved.


Language Fundamentals

Copyright © Prof. Jack Chao. All rights reserved.


Character and Lexical Units
Delimiters:
Assignment:
:=

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;

User defined variables, subroutines, and data types:


This is where we create all the identifiers.
Character and Lexical Units
Literals:
Character literals: a char(1) := ‘a’;

String literals: str varchar2(20) := ‘some thing’;

Numeric literals: n number := 2525; d double := 2.0d;

Boolean literals: b1 boolean := True; b2 boolean := false;

Date and Time literals: a_date date := ’01-Jun-07’;


Character and Lexical Units
Comments:
Single line comment:

-- this is a single line comment
a := ‘abc’;

Multiple line comment:



/* ----------------------------------------
This is multiple line comment
A program should have comments.
----------------------------------------- */
IF (v1 = 1) then

Block Structures
Anonymous block:
[DECLARE]

BEGIN

[EXCEPTION]

END;
/

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’;

LONG and LONG RAW:


avoid to use.

ROWID and UROWID:

DATE:
dt date := sysdate;

INTERVAL DAY TO SECOND:


intv interval day to second := ‘5 08:21:20’; /* ‘D HH:MI:SS’ */

INTERVAL YEAR TO MONTH:


intv interval year to month := ‘101-3’; /* ‘year-month’ */
intv interval year to month := interval ‘101-3’ year to month;
Variable Types
Scalar Datatypes:
TIMESTAMP:
ts1 timestamp := systimestamp;

TIMESTAMP WITH TIME ZONE:


TIMESTAMP WITH LOCAL TIME ZONE:

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:

• Associative Array (index by table)

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:

Ref cusor in SQLPLUS


VARIABLE refcur REFCURSOR

Ref cusor in PL/SQL block


DECLARE
type rc_type is ref cursor;
csr rc_type;
BEGIN
open csr for select item_title, count(*)
from item
group by item_title;
:refcur := csr;
END;
/

SELECT :refcur FROM dual;


Variable Scope

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

You might also like