You are on page 1of 7

Database Connectivity

Name : Atharva Mote


Roll no : 225
Batch : C4

Que:-Implement following different types of PL/SQL blocks

1. Anonymous PL/SQL block

2. Anonymous PL/SQL with cursor concept and Exception


handling

3. Anonymous PL/SQL with For loop cursor concept

4. Stored Procedure and Function

5. Trigger

1. Anonymous PL/SQL block


DECLARE
message varchar2(20):= 'Hello, World!'; BEGIN
dbms_output.put_line(message);
END;
/

--output
DECLARE
a integer := 45;
b integer := 12;
c integer; f
real;
BEGIN
c := a + b;
dbms_output.put_line('Value of c: ' || c);
f := 70.0/3.0;
dbms_output.put_line('Value of f: ' || f);
END;
/
--output

2. Anonymous PL/SQL with cursor concept and Exception


handling
DECLARE c_id
customers.id%type := 8; c_name
customerS.Name%type; c_addr
customers.address%type;
BEGIN
SELECT name, address INTO c_name, c_addr
FROM customers
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);

EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/

3. Anonymous PL/SQL with For loop cursor concept


PROBLEM STATEMENT: Write a PL/SQL Block which use cursor FOR
LOOP to select 5 highest earners from EMP table and write their
details into Message Table.

DECLARE v_salary
hr.employees.salary%TYPE; v_fname
hr.employees.first_name%TYPE;
CURSOR emp_cursor IS
SELECT salary, first_name , last_name
FROM hr.employees
Order by salary DESC;
BEGIN
FOR emp_sal in emp_cursor LOOP
EXIT WHEN emp_cursor%NOTFOUND OR emp_cursor%ROWCOUNT
= 6;
DBMS_OUTPUT.PUT_LINE (emp_sal.first_name ||' '||
emp_sal.last_name ||' :Rs '|| emp_sal.salary);
END LOOP;
END;

--output
4. Stored Procedure and Function
--Procedure
CREATE OR REPLACE PROCEDURE query_emp (p_amount IN
EMP.salary%Type, p_name IN Dept.name%Type )
IS
v_id number(3); BEGIN
select dep_id into v_id
from Dept where
name=p_name; update
EMP set
salary=salary+p_amount
where emp_id IN (select emp_id from works where dep_id=v_id);
END query_emp;
Select * from EMP

--function
CREATE OR REPLACE FUNCTION dname(
p_emp_id
IN Emp_emp.eid%TYPE
)
RETURN Dept_dept.dname%TYPE
IS
v_dname Dept_dept.dname%TYPE;
BEGIN
SELECT dname
INTO v_dname
FROM Dept_dept
WHERE did = (
SELECT did
FROM Works_works
WHERE eid = p_emp_id
);
RETURN v_dname;
END;
SELECT Emp_emp.eid, Emp_emp.ename, Emp_emp.age,
Emp_emp.salary , Dept_dept.did, Dept_dept.dname, Dept_dept.budget
FROM Emp_emp
FULL OUTER JOIN Dept_dept
ON Emp_emp.eid = Dept_dept.did; BEGIN

dbms_output.put_line('Department:

' || get_dname(02));

END;
--output
Function created.

5. Trigger
CREATE OR REPLACE TRIGGER display_salary_changes

BEFORE DELETE OR INSERT OR UPDATE ON customers

FOR EACH ROW


WHEN (NEW.ID > 0) DECLARE

sal_diff number; BEGIN sal_diff := :NEW.salary -

:OLD.salary; dbms_output.put_line('Old salary: ' ||

:OLD.salary); dbms_output.put_line('New salary: ' ||

:NEW.salary); dbms_output.put_line('Salary

difference: ' || sal_diff);

END;

--output
Trigger created.

CREATE OR REPLACE TRIGGER t


BEFORE
INSERT OR
UPDATE OF salary, department_id OR
DELETE
ON employees
BEGIN
CASE
WHEN INSERTING THEN
DBMS_OUTPUT.PUT_LINE('Inserting');
WHEN UPDATING('salary') THEN
DBMS_OUTPUT.PUT_LINE('Updating salary');
WHEN UPDATING('department_id') THEN
DBMS_OUTPUT.PUT_LINE('Updating department ID');
WHEN DELETING THEN
DBMS_OUTPUT.PUT_LINE('Deleting');
END CASE;
END;
/

--output
Trigger created.

You might also like