Writing Executable Statements
- Lexical Units in a PL/SQL Block
Lexical Units :
- Are building blocks of any PL/SQL Block
- Are sequences of characters including letters,numerals,tabs,spaces,returns
and symbols - Can be classified as:
- Identifiers
- Delimiters
- Literals
- Comments
- PL/SQL Block syntax and guidelines
- Literals:
- Character and Date Literals must be enclosed in single quotation marks.
Ex.
v_name VARCHAR2(40):=’Hudson’;
- Numbers can be simple values or scientific notation
- Statements can continue over several lines.
- Commenting Code
- Prefix single-line comments with two hypens (–)
- Place Multiple-line comments between the symbols /* and */
Ex.
— PL/SQL Block having Single Line Comments, Multiple Line Comments —
— Declare the Variables —
DECLARE
v_empno NUMBER;
BEGIN — Executable Section of a PL/SQL Block —
/*
PL-SQL Block to
print the Employee No. of an Employee Smith
*/
SELECT EMPNO
INTO v_EMPNO
FROM EMP
WHERE ENAME = ‘SMITH’;
DBMS_OUTPUT.PUT_LINE(‘Employee Number of SMITH is :’||v_empno);
END;
/
- SQL Functions in PL/SQL
a.SQL Functions available in Procedural Statements:
1. Single-Row Number
2. Single-Row character
3. Data Type Conversion
4. Date
5. Timestamp
6. GREATEST and LEAST
7. Miscellaneous Functions
b.Not available in Procedural Statements:
1. DECODE
2. Group Functions
Ex.
— PL/SQL Block to display the Length of a String and Convert the Employee Name LOWER Case —
DECLARE
DESC_SIZE INTEGER(5);
PROD_DESCRIPTION VARCHAR2(70):=’You can use this Product with your Radio Frequency’;
emp_name VARCHAR2(15):=’SMITH’;
BEGIN
— Get the Length of the String in PROD Description —
desc_size:=LENGTH(PROD_DESCRIPTION);
DBMS_OUTPUT.PUT_LINE(‘Length of the String : ‘||desc_size);
emp_name:=LOWER(EMP_NAME);
DBMS_OUTPUT.PUT_LINE(‘Lower Case of Employee Name : ‘||emp_name);
END;
/
— PL/SQL Block to display the value using the GREATEST and LEAST SQL Functions —
DECLARE
v_great_result NUMBER;
v_least_result NUMBER;
BEGIN
SELECT GREATEST(100,200,300) INTO
v_great_result FROM DUAL;
SELECT LEAST(100,200,300) INTO
v_least_result FROM DUAL;
DBMS_OUTPUT.PUT_LINE('Value returned by the GREATEST Function : '||v_great_result);
DBMS_OUTPUT.PUT_LINE('Value returned by the LEAST Function : '||v_least_result);
END;
/
- Data Type Conversion
- Convert Data to comparable Data Types
- Data Type Conversions are of two types :
a. Implicit Conversions
b. Explicit Conversions - Some Conversion Functions :
a. TO_CHAR
b. TO_DATE
c. TO_NUMBER
d. TO_TIMESTAMP
Ex.
— PL-SQL Block to have the Datatype Conversion —
DECLARE
DATE_OF_JOIN_DT DATE:=’02-FEB-2000′;
DATE_OF_JOIN_TD DATE:=TO_DATE(‘January 05,2000′,’Month DD,YYYY’);
BEGIN
DBMS_OUTPUT.PUT_LINE(‘Date Variable displayed using DATE :’||DATE_OF_JOIN_DT);
DBMS_OUTPUT.PUT_LINE(‘Date Variable displayed using TO_DATE :’||DATE_OF_JOIN_TD);
END;
/
- Nested Blocks
PL-SQL Blocks can be nested.
a. An executable section (BEGIN…END) can contain nested blocks.
b. An Exception section can contain nested blocks.
Ex.
— PL-SQL Block having a Nested Block —
DECLARE
OUTER_VARIABLE VARCHAR2(150):=’Global Variable in Main PL-SQL Block’;
BEGIN
DECLARE
INNER_VARIABLE VARCHAR2(150):=’Local Variable inside 1st Inner Block’;
BEGIN
DBMS_OUTPUT.PUT_LINE(INNER_VARIABLE);
DBMS_OUTPUT.PUT_LINE(OUTER_VARIABLE);
END;
DBMS_OUTPUT.PUT_LINE(OUTER_VARIABLE);
END;
/
— This PL/SQL Block will error out because the Inner Block Variable cannot be accessed in the Outer Block —
DECLARE
OUTER_VARIABLE VARCHAR2(150):=’Global Variable in Main PL-SQL Block’;
BEGIN
DECLARE
INNER_VARIABLE VARCHAR2(150):=’Local Variable inside 1st Inner Block’;
BEGIN
DBMS_OUTPUT.PUT_LINE(INNER_VARIABLE);
DBMS_OUTPUT.PUT_LINE(OUTER_VARIABLE);
END;
DBMS_OUTPUT.PUT_LINE(INNER_VARIABLE);
DBMS_OUTPUT.PUT_LINE(OUTER_VARIABLE);
END;
/
In the above PL/SQL Block the OUTER_VARIABLE Variable can be accessed anywhere inside any of the Inner Blocks defined in the Main PL/SQL Block.
i.e 1:n.. no.f of Inner Blocks.
But the Inner Variables scope is limited only to the Block where it is defined.
- Variable Scope and Visibility
Ex.
DECLARE
FATHER_NAME VARCHAR2(20):=’Patrick’;
DATE_OF_BIRTH DATE:=’20-APR-2012′;
BEGIN
DECLARE
CHILD_NAME VARCHAR2(20):=’Mike’;
DATE_OF_BIRTH DATE:=’12-DEC-2012′;
BEGIN
DBMS_OUTPUT.PUT_LINE(‘Father Name : ‘||father_name);
DBMS_OUTPUT.PUT_LINE(‘Date of Birth : ‘||date_of_birth);
DBMS_OUTPUT.PUT_LINE(‘Child Name : ‘||child_name);
END;
DBMS_OUTPUT.PUT_LINE(‘Date of Birth :’||date_of_birth);
END;
/
DECLARE
FATHER_NAME VARCHAR2(20):=’Patrick’;
DATE_OF_BIRTH DATE:=’20-APR-2012′;
BEGIN
DECLARE
CHILD_NAME VARCHAR2(20):=’Mike’;
DATE_OF_BIRTH DATE:=’12-DEC-2012′;
BEGIN
DBMS_OUTPUT.PUT_LINE(‘Father Name : ‘||father_name);
DBMS_OUTPUT.PUT_LINE(‘Date of Birth : ‘||date_of_birth);
DBMS_OUTPUT.PUT_LINE(‘Child Name : ‘||child_name);
END;
DECLARE
CHILD_NAME VARCHAR2(20):=’Mike’;
DATE_OF_BIRTH DATE:=’13-DEC-2012′;
BEGIN
DBMS_OUTPUT.PUT_LINE(‘Father Name : ‘||father_name);
DBMS_OUTPUT.PUT_LINE(‘Date of Birth : ‘||date_of_birth);
DBMS_OUTPUT.PUT_LINE(‘Child Name : ‘||child_name);
END;
DBMS_OUTPUT.PUT_LINE(‘Date of Birth :’||date_of_birth);
END;
/
In the above PL/SQL Block
- The Variable Father’s Name declared in the Main Block will fetch the value as ‘Patrick’.
- The Variable Date of Birth declared in Main Block and Inner Block will fetch the value as 12-DEC-2012 when referred inside the Inner Block.
When the same is referred in the Main Block it will fetch the value
as ’20-APR-2012.
- Qualify an Identifier
Ex.
— PL/SQL Block using the CONSTANT keyword for a Variable in the Main Block
— And the Same Variable Name in the Inner Block —
— Beginning of Main Block —
DECLARE
father_name CONSTANT VARCHAR2(20):=’Patrick’;
date_of_birth DATE:=’20-APR-1972′;
BEGIN
— Beginning of Inner Block —
DECLARE
father_name VARCHAR2(20):=’Peter’;
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;
/
— PL/SQL Block using the CONSTANT keyword for a Variable in the Main Block
— And the Same Variable Name in the Inner Block —
— And giving a different value for the CONSTANT Variable in the Main Block —
— This PL/SQL Block will error out —
— Beginning of Main Block —
DECLARE
father_name CONSTANT VARCHAR2(20):=’Patrick’;
date_of_birth DATE:=’20-APR-1972′;
BEGIN
father_name:=’Steven’;
DBMS_OUTPUT.PUT_LINE(‘Father”s Name: ‘||father_name);
— Beginning of Inner Block —
DECLARE
father_name VARCHAR2(20):=’Peter’;
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;
/
— PL-SQL Block to qualify an Identifier —
<>
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;
/
In the above PL/SQL Block the Variable declared in the Main Block can be referred inside the Inner PL/SQL Block using the Identifiers.
The Main PL/SQL Block is given a name an identifier ‘outer’.
Using this identifier the variables can be referred inside the Inner Block normally without the use the identifier it would’nt have been possible.
- Operators in PL/SQL
- Logical Operators
- Arithmetic Operators
- Concatenation Operators
- Parentheses to control order of operations
- Exponential Operator (**)
The following Operators : Logical,Arithmetic,Concatenation are most commonly used in SQL and PLSQL too.
The Exponential Operator can only be used in PLSQL and not in SQL.
Ex.
- Increment the counter for a Loop
loop_count:=loop_count + 1; - Set the Range Value for a Salary Variable
good_sal:=sal BETWEEN 50000 AND 150000 - Validate whether an Employee No.contains a Value
valid :=(empno IS NOT NULL);
— Example to demonstrate a Exponential Operator in PL/SQL —
DECLARE
a NUMBER:=3;
b NUMBER:=2;
c_e NUMBER;
c_p NUMBER;
BEGIN
c_e:= a ** b; — 3 to the power of 2 (Very similiar to power(m,n)) —
c_p:=power(a,b);
DBMS_OUTPUT.PUT_LINE(‘Result out of Exponential Operator is : ‘||c_e);
DBMS_OUTPUT.PUT_LINE(‘Result out of Exponential Operator is : ‘||c_p);
END;
/
- Programming Guidelines
Make Code Maintenance easier by :
- Documenting code with comments
- Developing a case convention for the code
- Developing naming conventions for identifiers and other objects
- Enchancing readability by indenting
- Indenting Code
For Clarity,indent each level of Code
Ex.
BEGIN
IF x=0 THEN
Y:=1;
END IF;
END;
/
DECLARE
deptno dept.deptno%TYPE;
location_id dept.loc%TYPE;
BEGIN
SELECT deptno,
loc
INTO deptno,
location_id
FROM dept
WHERE dname= ‘SALES’;
END;
/