You are on page 1of 9

Write a PL/SQL block to describe the usage of NULL values in equal

comparison, unequal comparison and NOT  NULL equals NULL comparison.

DECLARE
m NUMBER := 7;
n NUMBER := NULL;

o NUMBER := NULL;
p NUMBER := NULL;

q INTEGER := 4;
r INTEGER := 9;

large INTEGER;
----------------------------------
BEGIN
IF m != n THEN -- yields NULL, not TRUE
DBMS_OUTPUT.PUT_LINE('m != n'); -- not run
ELSIF m = n THEN -- also yields NULL
DBMS_OUTPUT.PUT_LINE('m = n');
ELSE
DBMS_OUTPUT.PUT_LINE
('Can not say whether m and n are equal or not.');
END IF;
-----------------------------------
IF o = p THEN -- yields NULL, not TRUE
DBMS_OUTPUT.PUT_LINE('o = p'); -- not run
ELSIF o != p THEN -- yields NULL, not TRUE
DBMS_OUTPUT.PUT_LINE('o != p'); -- not run
ELSE
DBMS_OUTPUT.PUT_LINE('Can not say whether two NULLs are equal');
END IF;
--------------------------------------
IF (q > r) -- If q or r is NULL, then (q > r) is NULL
THEN large := q; -- run if (q > r) is TRUE
ELSE large := r; -- run if (q > r) is FALSE or NULL
DBMS_OUTPUT.PUT_LINE('The value of large : '||large);
END IF;
IF NOT (q > r) -- If q or r is NULL, then NOT (q > r) is NULL
THEN large := r; -- run if NOT (q > r) is TRUE
ELSE large := q; -- run if NOT (q > r) is FALSE or NULL
DBMS_OUTPUT.PUT_LINE('The value of large : '||large);
END IF;
END;
/
Write a PL/SQL block to adjust the salary of the employee whose ID 122.
DECLARE
salary_of_emp NUMBER(8,2);

PROCEDURE approx_salary (
emp NUMBER,
empsal IN OUT NUMBER,
addless NUMBER
) IS
BEGIN
empsal := empsal + addless;
END;

BEGIN
SELECT salary INTO salary_of_emp
FROM employees
WHERE employee_id = 122;

DBMS_OUTPUT.PUT_LINE
('Before invoking procedure, salary_of_emp: ' || salary_of_emp);

approx_salary (100, salary_of_emp, 1000);

DBMS_OUTPUT.PUT_LINE
('After invoking procedure, salary_of_emp: ' || salary_of_emp);
END;
/
Result:
Before invoking procedure, salary_of_emp: 7900
After invoking procedure, salary_of_emp: 8900
Write a PL/SQL block to show single and multiline comments.
DECLARE
some_condition BOOLEAN;
pi NUMBER := 3.1415926; -- the value of pi is 3.1415926
: this is single line comment
radius NUMBER := 10;
area NUMBER;
BEGIN

IF 2 + 2 = 4 THEN
some_condition := TRUE;
/* IF is the simple control flow statement : this is multi line
comment*/
END IF;

/* The line below in the statement computes the area of a circle.


After the area is computed, the result is displayed. : : this is
multi line comment*/

area := pi * radius**2;
DBMS_OUTPUT.PUT_LINE('The area of the circle is: ' || area);
END;
/

Write a PL/SQL block to calculate the incentive of an employee whose ID is 110.


DECLARE
incentive NUMBER(8,2);
BEGIN
SELECT salary * 0.12 INTO incentive
FROM employees
WHERE employee_id = 110;
DBMS_OUTPUT.PUT_LINE('Incentive = ' || TO_CHAR(incentive));
END;
/
Result:
Incentive = 984
Write a PL/SQL program to show the uses of SIMPLE_INTEGER datatype.
DECLARE
num SIMPLE_INTEGER := 2147483645;
BEGIN
FOR j IN 1..4 LOOP
num := num + 1;
DBMS_OUTPUT.PUT_LINE(TO_CHAR(num, 'S9999999999'));
END LOOP;
FOR j IN 1..4 LOOP
num := num - 1;
DBMS_OUTPUT.PUT_LINE(TO_CHAR(num, 'S9999999999'));
END LOOP;
END;
/
Result:
+2147483646
+2147483647
-2147483648
-2147483647
-2147483648
+2147483647
+2147483646
+2147483645

PL/SQL procedure successfully completed.

PL SQL Cursor And Strings: Tutorial With Code


Examples
There are two types of cursors which are listed below:
1. Implicit Cursor
2. Explicit cursor
3. We have created a table with the SQL statement given below:
CREATE TABLE TUTOR(
CODE INT NOT NULL,
SUBJECT VARCHAR(15) NOT NULL,
TEACHER VARCHAR(15),
REVIEWS VARCHAR (10) NOT NULL,
PRIMARY KEY (CODE)
);
4. Inserted values to this table with SQL statements given below:
INSERT INTO TUTOR (CODE,SUBJECT,TEACHER,REVIEWS)
VALUES (1, 'Automation', 'Mukul', 'five stars');
INSERT INTO TUTOR (CODE,SUBJECT,TEACHER,REVIEWS)
VALUES (4, 'PLSQL', 'Anand', 'four stars');
INSERT INTO TUTOR (CODE,SUBJECT,TEACHER,REVIEWS)
VALUES (2, 'Performance', 'Arvind', 'four stars');
5. Code implementation with the implicit cursor:
DECLARE
    total_count number(30);
BEGIN
 
    --updating a row
    UPDATE TUTOR
    SET TEACHER = 'Zen' where CODE = 1;
 
    -- result in boolean, true returned if no rows affected
    IF sql%notfound THEN
        dbms_output.put_line('no subjects fetched');
 
 
        -- result in boolean, true returned if any rows affected
        ELSIF sql%found THEN
 
        -- count the number of rows affected rows affected
        total_count := sql%rowcount;
        dbms_output.put_line( total_count || ' teacher name updated ');
    END IF;
END;
/

The output of the above code should be:

Explicit Cursors
The developers can have their own user-defined context area to run DML operations. Thus
they can exercise more power over it. The declaration section of the PL/SQL block of code
contains explicit cursors. It is normally built on SELECT operations that fetch multiple rows.
Syntax of explicit cursor:
DECLARE

CURSOR <<cursor name>> IS <<select statement>>

<<Cursor variable>>

BEGIN

OPEN <<cursor name>>;

FETCH <<cursor name>> INTO <Cursor variable>;

CLOSE <cursor name>;

END;

Code implementation with explicit cursor:


DECLARE
   -- cursor declaration
CURSOR t_tutorials is
SELECT code, subject, teacher FROM Tutor;
t_code Tutor.code%type;
t_subject Tutor.subject%type;
t_teacher Tutor.teacher%type;
BEGIN
 
   -- opening a cursor
   OPEN t_tutorials;
LOOP
 
    -- fetching values from cursor
    FETCH t_tutorials into t_code, t_subject, t_teacher;
    EXIT WHEN t_tutorials%notfound;
 
    -- printing in console
dbms_output.put_line('Code is: ' || t_code || ' ' || 'Subject is: ' || t_subject ||
' Teacher is: ' || t_teacher);
END LOOP;
CLOSE t_tutorials;
END;
/
The output of the above code should be:

The syntax for Cursor For loop:


DECLARE
CURSOR c IS
SELECT code, subject, price FROM Tutorial;
...
BEGIN
FOR Tutorial_rec IN c LOOP
...
price_sum:= price_sum + Tutorial_rec.price;
END LOOP;

Code Implementation of Cursor based record, based on the Student table:


DECLARE
   CURSOR student_c is
      SELECT code, name, age 
      FROM student;
   student_r student_c%rowtype;
BEGIN
   OPEN student_c;
   LOOP
      FETCH student_c into student_r;
      EXIT WHEN student_c%notfound;
      DBMS_OUTPUT.put_line (student_r.code || ' ' || student_r.name);
   END LOOP;
END;
/  
The output of the above code:
PL/SQL Block
-- available online in file 'sample1'
DECLARE
x NUMBER := 100;
BEGIN
FOR i IN 1..10 LOOP
IF MOD(i,2) = 0 THEN -- i is even
INSERT INTO temp VALUES (i, x, 'i is even');
ELSE
INSERT INTO temp VALUES (i, x, 'i is odd');
END IF;
x := x + 100;
END LOOP;
COMMIT;
END;
Output Table
SQL> SELECT * FROM temp ORDER BY col1;

NUM_COL1 NUM_COL2 CHAR_COL


-------- -------- ---------
1 100 i is odd
2 200 i is even
3 300 i is odd
4 400 i is even
5 500 i is odd
6 600 i is even
7 700 i is odd
8 800 i is even
9 900 i is odd
10 1000 i is even

Sample 2. Cursors

The following example uses a cursor to select the five highest paid employees from the emp table.

Input Table
SQL> SELECT ename, empno, sal FROM emp ORDER BY sal DESC;

ENAME EMPNO SAL


---------- --------- --------
KING 7839 5000
SCOTT 7788 3000
FORD 7902 3000
JONES 7566 2975
BLAKE 7698 2850
CLARK 7782 2450
ALLEN 7499 1600
TURNER 7844 1500
MILLER 7934 1300
WARD 7521 1250
MARTIN 7654 1250
ADAMS 7876 1100
JAMES 7900 950
SMITH 7369 800
PL/SQL Block
-- available online in file 'sample2'
DECLARE
CURSOR c1 is
SELECT ename, empno, sal FROM emp
ORDER BY sal DESC; -- start with highest paid employee
my_ename VARCHAR2(10);
my_empno NUMBER(4);
my_sal NUMBER(7,2);
BEGIN
OPEN c1;
FOR i IN 1..5 LOOP
FETCH c1 INTO my_ename, my_empno, my_sal;
EXIT WHEN c1%NOTFOUND; /* in case the number requested */
/* is more than the total */
/* number of employees */
INSERT INTO temp VALUES (my_sal, my_empno, my_ename);
COMMIT;
END LOOP;
CLOSE c1;
END;
Output Table
SQL> SELECT * FROM temp ORDER BY col1 DESC;

NUM_COL1 NUM_COL2 CHAR_COL


-------- -------- --------
5000 7839 KING
3000 7902 FORD
3000 7788 SCOTT
2975 7566 JONES
2850 7698 BLAKE

You might also like