0% found this document useful (0 votes)
13 views8 pages

PLSQL

Uploaded by

ojaswigahoi2022
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
13 views8 pages

PLSQL

Uploaded by

ojaswigahoi2022
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

PL/SQL PRACTICE ASSIGNMENT

22BCE10783
OJASWI GAHOI

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


The following PL/SQL block shows a veriable incentive have been declared and incentive have
assigned into this variable.
Sample Solution:
Table: employees
employee_id integer
first_name varchar(25)
last_name varchar(25)
email archar(25)
phone_number varchar(15)
hire_date date
job_id varchar(25)
salary integer
commission_pct decimal(5,2)
manager_id integer
department_id integer

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;
/

2) Write a PL/SQL block to show an invalid case-insensitive reference to a quoted and


without quoted user-defined identifier.

DECLARE
"WELCOME" varchar2(10) := 'welcome'; -- identifier with quotation
BEGIN
DBMS_Output.Put_Line("Welcome"); --reference to the identifier with quotation and different case
END;
/

3) Write a PL/SQL block to show a reserved word can be used as a user-define


identifier.

DECLARE
"DECLARE" varchar2(25) := 'This is UPPERCASE';
"Declare" varchar2(25) := 'This is Proper Case';
"declare" varchar2(25) := 'This is lowercase';
BEGIN
DBMS_Output.Put_Line("DECLARE");
DBMS_Output.Put_Line("Declare");
DBMS_Output.Put_Line("declare");
END;
/

4) Write a PL/SQL block to show the result to neglect double quotation marks in
reserved word identifier.

DECLARE
"WORLD" varchar2(20) := 'world'; -- WORLD is not a reserved word
"DECLARE" varchar2(20) := 'declare'; -- DECLARE is a reserved word
BEGIN
DBMS_Output.Put_Line(World); -- Double quotation marks are optional
DBMS_Output.Put_Line(DECLARE); -- Double quotation marks are required
end;
/

5) Write a PL/SQL block to show the result to neglect the case sensitivity of a user
defined identifier which is also a reserved word.

DECLARE
"WORLD" varchar2(10) := 'world'; -- WORLD is not a reserved word
"DECLARE" varchar2(10) := 'declare'; -- DECLARE is a reserved word
BEGIN
DBMS_Output.Put_Line(World); -- Identifier is case-insensitive
DBMS_Output.Put_Line("Declare"); -- Identifier is case-sensitive
end;
/

6) Write a PL/SQL program to arrange the number of two variable in such a way that
the small number will store in num_small variable and large number will store in
num_large variable.

DECLARE
num_small NUMBER := 8;
num_large NUMBER := 5;
num_temp NUMBER;
BEGIN
IF num_small > num_large THEN
num_temp := num_small;
num_small := num_large;
num_large := num_temp;
END IF;
DBMS_OUTPUT.PUT_LINE ('num_small = '||num_small);
DBMS_OUTPUT.PUT_LINE ('num_large = '||num_large);
END;
/

7) Write a PL/SQL procedure to calculate the incentive on a target achieved and display
the message either the record updated or not.

DECLARE
PROCEDURE test1 (
sal_achieve NUMBER,
target_qty NUMBER,
emp_id NUMBER
)
IS
incentive NUMBER := 0;
updated VARCHAR2(3) := 'No';
BEGIN
IF sal_achieve > (target_qty + 200) THEN
incentive := (sal_achieve - target_qty)/4;

UPDATE emp
SET salary = salary + incentive
WHERE employee_id = emp_id;

updated := 'Yes';
END IF;

DBMS_OUTPUT.PUT_LINE (
'Table updated? ' || updated || ', ' ||
'incentive = ' || incentive || '.'
);
END test1;
BEGIN
test1(2300, 2000, 144);
test1(3600, 3000, 145);
END;
/

8) Write a PL/SQL program to check whether a number is even or odd.

DECLARE
-- Declare variable n, s, r, len
-- and m of datatype number
n NUMBER := 1634;
r NUMBER;
BEGIN
-- Calculating modulo
r := MOD(n, 2);

IF r = 0 THEN
dbms_output.Put_line('Even');
ELSE
dbms_output.Put_line('Odd');
END IF;
END;
/

9) Write a program in PL/SQL to print the value of a variable inside and outside a loop
using LOOP EXIT statement.

DECLARE
n NUMBER := 0;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('The value of n inside the loop is: ' || TO_CHAR(n));
n := n + 1;
EXIT WHEN n > 5;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The value of n after exit from the loop is: ' || TO_CHAR(n));
END;
/

10) Write a program in PL/SQL to update the salary of a specifc employee by 8% if the
salary exceeds the mid range of the salary against this job and update up to mid
range if the salary is less than the mid range of the salary, and display a suitable
message.

DECLARE
emp_min_salary NUMBER(6,0);
emp_max_salary NUMBER(6,0);
emp_mid_salary NUMBER(6,2);
tmp_salary EMPLOYEES.SALARY%TYPE;
tmp_emp_id EMPLOYEES.EMPLOYEE_ID%TYPE := 167;
tmp_emp_name EMPLOYEES.FIRST_NAME%TYPE;
BEGIN

SELECT min_salary,
max_salary
INTO emp_min_salary,
emp_max_salary
FROM JOBS
WHERE JOB_ID = (SELECT JOB_ID
FROM EMPLOYEES
WHERE EMPLOYEE_ID = tmp_emp_id);

-- calculate mid-range
emp_mid_salary := (emp_min_salary + emp_max_salary) / 2;
-- get salary of the given employee
SELECT salary,first_name
INTO tmp_salary,tmp_emp_name
FROM employees
WHERE employee_id = tmp_emp_id;

-- update salary

IF tmp_salary < emp_mid_salary THEN


UPDATE employees
SET salary = emp_mid_salary
WHERE employee_id = tmp_emp_id;
ELSE
UPDATE employees
SET salary = salary + salary * 8 /100
WHERE employee_id = tmp_emp_id;
END IF;
--display message
IF tmp_salary > emp_mid_salary THEN
DBMS_OUTPUT.PUT_LINE('The employee '||tmp_emp_name||' ID ' || TO_CHAR(tmp_emp_id) ||
' works in salary ' || TO_CHAR(tmp_salary) ||
' which is higher than mid-range of salary ' || TO_CHAR(emp_mid_salary));
ELSIF tmp_salary < emp_mid_salary THEN
DBMS_OUTPUT.PUT_LINE('The employee '||tmp_emp_name||' ID ' || TO_CHAR(tmp_emp_id) ||
' works in salary ' || TO_CHAR(tmp_salary) ||
' which is lower than mid-range of salary ' || TO_CHAR(emp_mid_salary));

ELSE
DBMS_OUTPUT.PUT_LINE('The employee '||tmp_emp_name||' ID ' || TO_CHAR(tmp_emp_id) ||
' works in salary ' || TO_CHAR(tmp_salary) ||
' which is equal to the mid-range of salary ' || TO_CHAR(emp_mid_salary));
END IF;
END;
/

11) Write a PL/SQL program to arrange the number of two variable in such a way that
the small number will store in num_small variable and large number will store in
num_large variable.

DECLARE
num_small NUMBER := 8;
num_large NUMBER := 5;
num_temp NUMBER;
BEGIN

IF num_small > num_large THEN


num_temp := num_small;
num_small := num_large;
num_large := num_temp;
END IF;

DBMS_OUTPUT.PUT_LINE ('num_small = '||num_small);


DBMS_OUTPUT.PUT_LINE ('num_large = '||num_large);
END;
/

12) Write a PL/SQL procedure to calculate the incentive on a specific target otherwise a
general incentive to be paid using IF-THEN-ELSE.

DECLARE
PROCEDURE test1 (
sal_achieve NUMBER,
target_qty NUMBER,
emp_id NUMBER
)
IS
incentive NUMBER := 0;

BEGIN
IF sal_achieve > (target_qty + 200) THEN
incentive := (sal_achieve - target_qty)/4;
ELSE
incentive :=75;
END IF;
DBMS_OUTPUT.PUT_LINE ('incentive = ' || incentive);
UPDATE emp
SET salary = salary + incentive
WHERE employee_id = emp_id;

END test1;
BEGIN
test1(2300, 2000, 144);
test1(3600, 3000, 145);
END;
/

13) Write a PL/SQL program to count number of employees in a specific department and
check whether this department have any vacancies or not. If any vacancies, how
many vacancies are in that department.
SET SERVEROUTPUT ON
DECLARE
tot_emp NUMBER;
BEGIN
SELECT Count(*)
INTO tot_emp
FROM employees e
join departments d
ON e.department_id = d.department_id
WHERE e.department_id = 50;

dbms_output.Put_line ('The employees are in the department 50: '


||To_char(tot_emp));

IF tot_emp >= 45 THEN


dbms_output.Put_line ('There are no vacancies in the department 50.');
ELSE
dbms_output.Put_line ('There are some vacancies in department 50.');
END IF;
END;
/

14) Write a program in PL/SQL to print the value of a variable inside and outside a loop
using LOOP WHEN EXIT statement.

DECLARE
n NUMBER := 0;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE ('The value of n inside the loop is: ' || TO_CHAR(n));
n := n + 1;
IF n > 5 THEN
EXIT;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The value of n outside the loop is: ' || TO_CHAR(n));
END;
/

15) Write a program in PL/SQL to explain the uses of nested for loop with label.

BEGIN
<>
FOR j IN 1..5 LOOP
<<loop_inner>>
FOR j IN 1..4 LOOP
IF loop_outer.j = 4 THEN
DBMS_OUTPUT.PUT_LINE
('When the value of j of outer loop: ' || TO_CHAR(loop_outer.j) || ' then the value of j in the
inner loop: '
|| TO_CHAR(loop_inner.j));
END IF;
END LOOP loop_inner;
END LOOP loop_outer;
END;
/

You might also like