Writing Executable Statements
Writing Executable Statements
Lexical units:
• Are building blocks of any PL/SQL block
• Are sequences of characters including letters,
digits, tabs, spaces, returns, and symbols
• Can be classified as:
– Identifiers
– Delimiters
– Literals
– Comments
• Literals:
– Character and date literals must be enclosed in
single quotation marks.
name := 'Henderson';
– Numbers can be simple values or scientific
notation.
• Statements can continue over several lines.
Example:
DECLARE
outer_variable VARCHAR2(20):='GLOBAL VARIABLE';
BEGIN
DECLARE
inner_variable VARCHAR2(20):='LOCAL VARIABLE';
BEGIN
DBMS_OUTPUT.PUT_LINE(inner_variable);
DBMS_OUTPUT.PUT_LINE(outer_variable);
END;
DBMS_OUTPUT.PUT_LINE(outer_variable);
END;
/
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);
1 DBMS_OUTPUT.PUT_LINE('Date of Birth: '||date_of_birth);
DBMS_OUTPUT.PUT_LINE('Child''s Name: '||child_name);
END;
2 DBMS_OUTPUT.PUT_LINE('Date of Birth: '||date_of_birth);
END;
/
<<outer>>
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: '
||outer.date_of_birth);
DBMS_OUTPUT.PUT_LINE('Child''s Name: '||child_name);
DBMS_OUTPUT.PUT_LINE('Date of Birth: '||date_of_birth);
END;
END;
/`
<<outer>>
DECLARE
sal NUMBER(7,2) := 60000;
comm NUMBER(7,2) := sal * 0.20;
message VARCHAR2(255) := ' eligible for commission';
BEGIN
DECLARE
sal NUMBER(7,2) := 50000;
comm NUMBER(7,2) := 0;
total_comp NUMBER(7,2) := sal + comm;
BEGIN
message := 'CLERK not'||message;
1 outer.comm := sal * 0.30;
END;
message := 'SALESMAN'||message;
2
END;
/
• Logical
• Arithmetic
• Concatenation Same as in SQL
• Parentheses to control order
of operations
Examples:
• Increment the counter for a loop.
loop_count := loop_count + 1;