Académique Documents
Professionnel Documents
Culture Documents
Objectives:
By the end of this lab, students will be able to use PL/SQL variables and operators.
Pre-Requisite:
Knowledge of basic SQL commands.
PL/SQL Variables
value or DEFAULT value is also an optional specification, where you can initialize a
variable.
Note that, when you use NOT NULL constraint, then you must explicitly assign an initial value to the
variable. Also note that you may either initialize variables in Declaration section or in Execution
section of PL/SQL block.
DECLARE
v_first_name EMPLOYEES.FIRST_NAME%TYPE;
v_last_name EMPLOYEES.LAST_NAME%TYPE;
n_employee_id EMPLOYEES.EMPLOYEE_ID%TYPE;
d_hire_date EMPLOYEES.HIRE_DATE%TYPE;
BEGIN
NULL;
END;
/
In the above declaration section, the variable v_frst_name has the data type as first_name (column
name) in employees table. If the data type of first_name changes in table, then PL/SQL will
automatically update the data type of v_first_name in the program.
SET SERVEROUTPUT ON
DECLARE
v_first_name EMPLOYEES.FIRST_NAME%TYPE;
v_last_name EMPLOYEES.LAST_NAME%TYPE;
n_employee_id EMPLOYEES.EMPLOYEE_ID%TYPE;
d_hire_date EMPLOYEES.HIRE_DATE%TYPE;
BEGIN
SELECT employee_id,
first_name,
last_name,
hire_date
INTO
n_employee_id,
v_first_name,
v_last_name,
d_hire_date
FROM employees
WHERE employee_id = 200;
DBMS_OUTPUT.PUT_LINE(v_first_name);
DBMS_OUTPUT.PUT_LINE(v_last_name);
DBMS_OUTPUT.PUT_LINE(d_hire_date);
END;
/
DECLARE
-- Global variables
num1 number := 95;
num2 number := 85;
BEGIN
dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
DECLARE
-- Local variables
num1 number := 195;
num2 number := 185;
BEGIN
dbms_output.put_line('Inner Variable num1: ' || num1);
dbms_output.put_line('Inner Variable num2: ' || num2);
END;
END;
/
DECLARE
num number := 10;
BEGIN
DECLARE
num number := 10;
BEGIN
IF num = OUTER.num THEN
DBMS_OUTPUT.PUT_LINE('Both have same value');
ELSE
DBMS_OUTPUT.PUT_LINE('Different value');
END IF;
END; -- End of scope to access num variable
END;
/
PL/SQL Operators
Arithmetic operators
Relational operators
Comparison operators
Logical operators
String operators
Arithmetic operators
The following table shows all the arithmetic operators that can be used in PL/SQL programs.
Operator
Description
Example
A + B will
give 15
A - B will
give 5
A * B will
give 50
A / B will
give 2
**
A ** B will
give
100000
Relational Operators
Relational operators are used to compare variables and they always return a Boolean value. The
following table shows relational operators that can be used in PL/SQL programs.
Operator
Description
Example
(A = B)
is not
true.
(A != B) is true.
>
(A > B)
is not
true.
<
(A < B)
is true.
>=
(A >= B)
is not
true.
<=
(A <= B)
is true.
Comparison Operators
These operators are used to compare expressions. The following table shows comparison operators
that can be used in PL/SQL programs.
Operator
Description
Example
LIKE
The LIKE operator compares a character, string, or CLOB value to a pattern and
returns TRUE if the value matches the pattern and FALSE if it does not.
If 'Zara Ali'
like 'Z% A_i'
returns a
Boolean true,
whereas,
'Nuha Ali' like
'Z% A_i'
returns a
Boolean false.
BETWEEN
If x = 10
then, x
between 5
and 20
returns true, x
between 5
and 10
returns true,
but x between
11 and 20
returns false.
IN
The IN operator tests set membership. x IN (set) means that x is equal to any
member of set.
If x = 'm'
then, x in ('a',
'b', 'c')
returns
boolean false
but x in ('m',
'n', 'o')
returns
Boolean true.
IS NULL
The IS NULL operator returns the BOOLEAN value TRUE if its operand is NULL or
FALSE if it is not NULL. Comparisons involving NULL values always yield NULL.
If x = 'm',
then 'x is null'
returns
Boolean false.
Logical Operators
Following table shows the Logical operators supported by PL/SQL. All these operators work on
Boolean operands and produces Boolean results. Assume variable A holds true and variable B holds
false, then:
Operator
Description
Example
and
Called logical AND operator. If both the operands are true then
condition becomes true.
(A and
B) is
false.
or
(A or B)
is true.
not
Called logical NOT Operator. Used to reverse the logical state of its
operand. If a condition is true then Logical NOT operator will make it
false.
not (A
and B) is
Operator
Operation
**
Exponentiation
+, -
identity, negation
*, /
multiplication, division
+, -, ||
Comparison
NOT
logical negation
AND
Conjunction
OR
Inclusion
Comments in PL/SQL
Comments in PL/SQL are used either with /* */ or -The /* and */ are used in tandem and can be used over multiple lines. They cannot be nested.
Everything between /* and */ is ignored.
The -- is used at the end of a line. Everything after the -- is ignored up to the carriage return.
Lab Tasks
First_nam
Last_Nam
Departme
DOB
d
200121
e
Ali
e
Haider
200122
Rizwan
Pasha
200123
Ibrahim
Ali
200124
Khawar
Hayyat
200125
Ali
Azmat
Ali_haider@gmail.com
Rizwan_pasha@hotmai
l.com
Ibrahim123@yahoo.co
m
Khawar321@yahoo.co
m
Ali_azmat@gmail.com
nt
CS
EE
Geology
CS
EE
01-Jun1993
06-July1994
14-Aug1994
25-Dec1994
9-Sep1994
Task 01:
Declare two variables and store emails of students in them. The
student ids for those students should be 200121 and 200122
respectively.
Task 02:
Create a PL/SQL block to find the age of the student whose student id
is 200123. (Hint: Use SYSDATE, DATE keyword, and Extract function
respectively.)
Task 03:
Create a PL/SQL block to delete the record of student whose id is
200125.
Task 04:
Write a PL/SQL block to update the email of student whose student id is
200121. Also display the previous and updated emails of student.
Task 05:
Write a function to find result of the following formula.
a2 +b 2+2 ab
INCLUDE Irvine32.inc
.data
a byte 0
ar2 dword 10 DUP(0)
.code
main PROC
mov eax , 0
mov esi , offset ar2
mov ecx, 10
Output:
call readint
mov a,ah
mov ah,a
mov [esi],ah
add esi,1
call writedec
call crlf
Loop Output
Exit
Main ENDP
END main