100% found this document useful (2 votes)
4K views4 pages

Handson PLSQL

The document contains PL/SQL code snippets demonstrating various SQL concepts like anonymous blocks, cursors, procedures, functions, packages and triggers. Some key operations shown include inserting records using anonymous blocks, updating records by selecting into variables, deleting records using conditions, displaying data using cursors, exception handling in procedures, returning values from functions, defining packages with procedures and updating data, and creating triggers for insert/update and delete operations.

Uploaded by

Keshav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
4K views4 pages

Handson PLSQL

The document contains PL/SQL code snippets demonstrating various SQL concepts like anonymous blocks, cursors, procedures, functions, packages and triggers. Some key operations shown include inserting records using anonymous blocks, updating records by selecting into variables, deleting records using conditions, displaying data using cursors, exception handling in procedures, returning values from functions, defining packages with procedures and updating data, and creating triggers for insert/update and delete operations.

Uploaded by

Keshav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

area of a circle

declare
V_RADIUS number(5);
V_AREA number(7,2);
pi constant number(3,2) :=3.14;
begin
V_RADIUS:=3;
while V_RADIUS<=7
loop
V_AREA:=pi*V_RADIUS*V_RADIUS;
insert into CIRCLE values(V_RADIUS,V_AREA );
V_RADIUS:=V_RADIUS+1;
end loop;
end;
/

-----------------------------------

Insert Record using Anonymous Block


declare
v_department_id number(5);
v_department_name varchar2(25);
v_location_id varchar2(15);
begin
v_department_name:='TESTING';
v_location_id:='CHN-102';
SELECT max(department_id)+10 into v_department_id from department;
insert into department values(v_department_id,v_department_name,v_location_id);
end;
/

------------------------------
Update Location
DECLARE
v_department_id number(5);
v_department_name varchar2(25);
v_location_id varchar2(15);
begin
select DEPARTMENT_ID,DEPARTMENT_NAME into v_department_id,v_department_name from
DEPARTMENT where LOCATION_ID LIKE 'HQ%';
UPDATE DEPARTMENT SET LOCATION_ID='HQ-BLR-101' WHERE DEPARTMENT_ID=v_department_id
AND DEPARTMENT_NAME=v_department_name;
end;
/

----------------------------

Delete Record(s) at a particular location


begin
DELETE FROM DEPARTMENT WHERE LOCATION_ID IN 'CHN-102';
END;
/

----------------------------------------

Display department names using Cursors


set serveroutput on;
declare
cursor c_dept is select department_name from department order by department_name;
v_dept c_dept%rowtype;
begin
open c_dept;
dbms_output.put_line('Department Names are :');
loop
fetch c_dept into v_dept;
exit when c_dept%notfound;
dbms_output.put_line(upper(v_dept.department_name));
end loop;
close c_dept;
end;
/

-----------------------------------

Department Highest Salary Expenditure

set serveroutput on;


declare
cursor c_emp is select dept,sum(salary) s from employee group by dept;
v_emp c_emp%rowtype;
begin
open c_emp;
dbms_output.put_line('Department-wise salary expenditure:');
loop
fetch c_emp into v_emp;
exit when c_emp%notfound;
dbms_output.put_line(v_emp.dept||' department, total salary is '||v_emp.s);
end loop;
close c_emp;
end;
/

------------------------------

Department Details
set serveroutput on;
declare
cursor c_dept is select * from department order by department_id;
v_dept c_dept%rowtype;
begin
open c_dept;
dbms_output.put_line('Department Details are :');
loop
fetch c_dept into v_dept;
exit when c_dept%notfound;
dbms_output.put_line(v_dept.department_id||', '||v_dept.Department_name||', '||
v_dept.location_id);
end loop;
close c_dept;
end;
/
-------------------------------------------
Procedure with exception handling

create or replace procedure check_age_eligibility(


v_id in employee.empid%type,
v_name in employee.empname%type,
v_age in employee.age%type
) as
in_valid_age exception;
begin
if v_age >=18 then
insert into employee values(v_id, v_name, v_age);
dbms_output.put_line('Age valid - Record inserted');
else
raise in_valid_age;
end if;
exception
when in_valid_age then
dbms_output.put_line('Age invalid - Record not inserted');
end;
/

---------------------------------------

Calculate increment using Function


create or replace function calculate_Increment(incperc number,salary number) return
number as
begin
return salary*incperc/100;
end;
/

---------------------------------------
Remove Employee records - Procedures
set serveroutput on;
create or replace procedure DELETE_EMPLOYEE(v_dept IN EMPLOYEE.DEPT%TYPE)
is
DeptNotFoundException exception;
begin
delete from employee where dept=v_dept;
if sql%found then
dbms_output.put_line(sql%rowcount||' Employee record(s) got deleted.');
else
raise DeptNotFoundException;
end if;
exception
when DeptNotFoundException then
dbms_output.put_line('No Records found.');
end;
/

------------------------

Concatenate Employee names - Functions


create or replace FUNCTION CONCAT_NAME(v_id employee.emp_id%type) return varchar2
asx varchar2(25);
begin
select first_name||last_name into x from employee where emp_id=v_id;
return x;
end;
/

-------------------------------------------------
Package with a Procedure to Update Salary

create or replace package EMP_DESIGNATION


as
procedure emp_details(design employee.designation%type,
incentive number);
end emp_designation;
/
create or replace package body emp_designation
as
procedure emp_details (design employee.designation%type, incentive number)
is
begin
update employee
set employee.salary = employee.salary + incentive
where employee.designation = design;
dbms_output.put_line (
sql%rowcount || ' employee(s) are updated.');
END emp_details;
END emp_designation;
/

-------------------------

insert a record-TRIGGER

CREATE OR REPLACE TRIGGER display_message


AFTER INSERT OR UPDATE ON employee
BEGIN
DBMS_OUTPUT.put_line ('NEW EMPLOYEE DETAILS INSERTED');
END;
/

------------------------------------
delete a record- TRIGGERS

create or replace trigger insert_delete


after delete ON employee
for each row
begin
insert into employee_archive(empid,empname,managerid,deleted_date)
values(:old.empid,:old.empname,:old.managerid,sysdate);
end;
/

You might also like