Lab 2. Introduction To PL/SQL and Cursors

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Lab 2.

Introduction to PL/SQL and Cursors

2.1 Identify the problems(if any) in the below declarations:


DECLARE V_Sample1 NUMBER(2);
V_Sample2 CONSTANT NUMBER(2) ;
V_Sample3 NUMBER(2) NOT NULL ;
V_Sample4 NUMBER(2) := 50;
V_Sample5 NUMBER(2) DEFAULT 25;

1- Declaration of a constant ‘V_SAMPLE2’ must contain an initialization assignment.


2- a variable declared NOT NULL must have an initialization assignment.

DECLARE
V_Sample1 NUMBER(2);
V_Sample2 CONSTANT NUMBER(2):=10;
V_Sample3 NUMBER(2) NOT NULL:=1 ;
V_Sample4 NUMBER(2) := 50;
V_Sample5 NUMBER(2) DEFAULT 25;
7 begin
8 dbms_output.put_line('proceed');
9 END;
10 /

2-
DECLARE --outer block
2 var_num1 NUMBER := 5;
3 BEGIN
4 DECLARE --inner block
5 var_num1 NUMBER := 10;
6 BEGIN
7 DBMS_OUTPUT.PUT_LINE('Value for var_num1:' ||var_num1);
8 DBMS_OUTPUT.PUT_LINE('Value for outer block var_num1:' ||var_num1);
9 end;
10 end;
11 /
2.3. Write a PL/SQL block to retrieve all staff (code, name, salary)
under specific department number and display the result. (Note: The
Department_Code will be accepted from user. Cursor to be used.)
DECLARE

C_REV STAFF_MASTERS%ROWTYPE;

CURSOR C_STAFF_MASTERS IS SELECT * FROM STAFF_MASTERS WHERE DEPT_CODE=&DEPT_CODE;

BEGIN

OPEN C_STAFF_MASTERS;

LOOP

FETCH C_STAFF_MASTERS INTO C_REV;

EXIT WHEN C_STAFF_MASTERS%NOTFOUND;

DBMS_OUTPUT.PUT_LINE(C_REV.STAFF_NAME||' '||C_REV.STAFF_CODE||' '||C_REV.STAFF_SAL);

END LOOP;

CLOSE C_STAFF_MASTERS;

END;

2.4. Write a PL/SQL block to increase the salary by 30 % or 5000


whichever minimum for a given Department_Code.
DECLARE

C1_SAL NUMBER(10);
C2_SAL NUMBER(10);

C_SAL STAFF_MASTERS.STAFF_SAL%TYPE;

CURSOR C_STAFF_MASTERS IS SELECT STAFF_SAL FROM STAFF_MASTERS WHERE


DEPT_CODE=&DEPT_CODE;

BEGIN

OPEN C_STAFF_MASTERS;

LOOP

FETCH C_STAFF_MASTERS INTO C_SAL;

EXIT WHEN C_STAFF_MASTERS%NOTFOUND;

C1_SAL:=C_SAL*0.3;

C2_SAL:=C_SAL+5000;

IF C1_SAL>C2_SAL THEN

UPDATE STAFF_MASTERS SET STAFF_SAL=C2_SAL;

ELSE

UPDATE STAFF_MASTERS SET STAFF_SAL=C1_SAL;

END IF;

DBMS_OUTPUT.PUT_LINE(C_SAL);

END LOOP;

CLOSE C_STAFF_MASTERS;

END;
OR WITHOUT CURSOR-

SQL> DECLARE
2 V_SAL STAFF_MASTERS.STAFF_SAL%TYPE;
3 V_DEPT STAFF_MASTERS.DEPT_CODE%TYPE;
4 BEGIN
5 SELECT MIN(STAFF_SAL) INTO V_SAL FROM STAFF_MASTERS WHERE
DEPT_CODE=&V_DEPT;
6 DBMS_OUTPUT.PUT_LINE('SALARY AFTER INCREMENT: '||V_SAL);
7 END;
8 /

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy