0% found this document useful (0 votes)
33 views14 pages

Cycle LLL

Uploaded by

priyaravella23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views14 pages

Cycle LLL

Uploaded by

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

Cycle-3(pl/sql)

1. The hrd manager has decided to raise the employee salary by 15%. Write a pl/sql block to accept
the employee number and update the salary of that employee. Display appropriate message
based on the existence of the record in emp table.

ANS. 7369 SMITH CLERK 7902 17-DEC-80 800 20

SQL> BEGIN

UPDATE EMP2 SET SAL=SAL+(SAL*15/100)

WHERE EMPNO=&EMPNO;

IF SQL % FOUND THEN

DBMS_OUTPUT.PUT_LINE('EMPLOYEE RECORD MODIFIED SUCCESSFULLY');

ELSE

DBMS_OUTPUT.PUT_LINE('EMPLOYEE DOES NOT EXIST');

END IF;

END;

Enter value for empno: 7369

old 3: WHERE EMPNO=&EMPNO;

new 3: WHERE EMPNO=7369;

PL/SQL procedure successfully completed.

SQL> SELECT * FROM EMP2 WHERE EMPNO=7369;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

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

7369 SMITH CLERK 7902 17-DEC-80 920 20

2. Write a pl/sql program to display top 10 rows in emp table based on their job and salary.

Ans. SQL> declare

cursor c1 is select * from emp2 order by sal desc;

e emp2%rowtype;

begin
Cycle-3(pl/sql)
open c1;

loop

fetch c1 into e;

exit when c1%rowcount=11 or c1%notfound;

dbms_output.put_line('top ::'||c1%rowcount||' employee');

dbms_output.put_line('employee no:'||e.empno);

dbms_output.put_line('employee name'||e.ename);

dbms_output.put_line('employee job is '||e.job);

dbms_output.put_line('employee salary is '||e.sal);

dbms_output.put_line('*-*-*-*-*-*-*-*-*-*-*-*-*-*');

end loop;

end;

PL/SQL procedure successfully completed.

SQL> SET SERVEROUTPUT ON;

top ::1 employee

employee no:7839

employee nameKING

employee job is PRESIDENT

employee salary is 5000

*-*-*-*-*-*-*-*-*-*-*-*-*-*

top ::2 employee

employee no:7788

employee nameSCOTT

employee job is ANALYST

employee salary is 3200

*-*-*-*-*-*-*-*-*-*-*-*-*-*

top ::3 employee

employee no:7902

employee nameFORD
Cycle-3(pl/sql)
employee job is ANALYST

employee salary is 3000

*-*-*-*-*-*-*-*-*-*-*-*-*-*

top ::4 employee

employee no:7566

employee nameJONES

employee job is MANAGER

employee salary is 2975

*-*-*-*-*-*-*-*-*-*-*-*-*-*

top ::5 employee

employee no:7698

employee nameBLAKE

employee job is MANAGER

employee salary is 2850

*-*-*-*-*-*-*-*-*-*-*-*-*-*

top ::6 employee

employee no:7782

employee nameCLARK

employee job is MANAGER

employee salary is 2450

*-*-*-*-*-*-*-*-*-*-*-*-*-*

top ::7 employee

employee no:7499

employee nameALLEN

employee job is SALESMAN

employee salary is 1600

*-*-*-*-*-*-*-*-*-*-*-*-*-*

top ::8 employee

employee no:7844

employee nameTURNER
Cycle-3(pl/sql)
employee job is SALESMAN

employee salary is 1500

*-*-*-*-*-*-*-*-*-*-*-*-*-*

top ::9 employee

employee no:7934

employee nameMILLER

employee job is CLERK

employee salary is 1300

*-*-*-*-*-*-*-*-*-*-*-*-*-*

top ::10 employee

employee no:7654

employee nameMARTIN

employee job is SALESMAN

employee salary is 1250

*-*-*-*-*-*-*-*-*-*-*-*-*-*

3. Write a pl/sql program to raise the employee salary by 10%, for department number 30 people
and also maintain the raised details in the raise table.

ANS. SQL>create table raise_emp

(empid number(4) primary key,

name varchar2(10),

desig varchar2(9),

mgr number(4),

doj date,

salary number(7,2),

comm number(7,2),

dno number(2));

Table created.

SQL> declare

cursor c2 is select * from emp2 where deptno=30 for update;

e emp2%rowtype;
Cycle-3(pl/sql)
begin

open c2;

loop

fetch c2 into e;

exit when c2%notfound;

update emp2 set sal=e.sal+(10/100)*e.sal where current of c2;

end loop;

dbms_output.put_line('employee details are stored in the emp_raise

table');

insert into raise_emp(select * from emp2 where deptno=30);

close c2;

end;

employee details are stored in the emp_raise

table

PL/SQL procedure successfully completed.

SQL> SET SERVEROUTPUT ON;

SQL> SELECT * FROM RAISE_EMP;

EMPID NAME DESIG MGR DOJ SALARY COMM DNO

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

7698 BLAKE MANAGER 7839 01-MAY-81 3135 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1375 1400 30

7499 ALLEN SALESMAN 7698 20-FEB-81 1760 300 30

7844 TURNER SALESMAN 7698 08-SEP-81 1650 0 30

7900 JAMES CLERK 7698 03-DEC-81 1045 30

7521 WARD SALESMAN 7698 22-FEB-81 1375 500 30

6 rows selected.
Cycle-3(pl/sql)
4. Write a pl/sql procedure to prepare an electricity bill by using following table used: elect

NAME PREV_READ

NULL? NUMBER(5)

TYPE NO_UNITS

MNO NUMBER(5)

NOT NULL AMOUNT

NUMBER(3) NUMBER(8,2)

CNAME SER_TAX

VARCHAR2(20) NUMBER(8,2)

CUR_READ NET_AMT

NUMBER(5)

NUMBER(9,2)

CREATE TABLE ELECT (………………..

…………………………………………….

create or replace procedure powerbill

as

no_un elect.no_units%type;

amt elect.amount%type;

stax elect.ser_tax%type;

net elect.net_amt%type;

e elect%rowtype;

cursor c9 is select * from elect for update;

begin

open c9;

loop

fetch c9 into e;

exit when c9%notfound;

no_un:=e.cur_read-e.prev_read;
Cycle-3(pl/sql)
amt:=no_un*1.5;

stax:=(5/100)*amt;

net:=amt+stax;

update elect set no_units=no_un,amount=amt,

ser_tax=stax,net_amt=net where current of c9;

dbms_output.put_line('customer meter number'||e.mno);

dbms_output.put_line('customer name'||e.cname);

dbms_output.put_line('total amount is '||net);

end loop;

close c9;

end;

SQL> Insert into elect(mno,cname,cur_read,prev_read) VALUES(1,'SAI',225,105);

1 row created.

……….

………..

………

SQL> EXECUTE POWERBILL;

PL/SQL procedure successfully completed.

SQL> SELECT * FROM ELECT;

MNO CNAME CUR_READ PREV_READ NO_UNITS AMOUNT SER_TAX NET_AMT

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

1 SAI 225 105 120 180 9 189

5. Write a pl/sql procedure to evaluate the grade of a student with following conditions: for
pass: all marks > 40 for i class: total%>59 for ii class: total%between>40 and <60 for iii class:
total%=40 Alsomaintain the details in abstract table. Tables USED
1. TABLE NULL? TYPE
STD NAME
---------------- -------- --------
NO NOT NULL NUMBER
NAME VARCHAR2(10)
INTNO NUMBER
CLASS NOT NULL VARCHAR
2(10)
M1 NUMBER
M2 NUMBER
Cycle-3(pl/sql)
M3 NUMBER
M4 NUMBER
M5 NUMBER

2. TABLE NULL? TYPE


ABSTRACT
NAME
---------------- -------- --------
STDNO NUMBER
STDNAME VARCHAR2(10)
CLASS VARCHAR2(10)
MONTH VARCHAR2(10)
INTNO (INTERNAL NUMBER
NUMBER)
TOT NUMBER
GRADE VARCHAR2(10)
PERCENT NUMBER
DAT_ENTER DATE

ANS. SQL> declare

cursor stu_c is select *from std;

s1 std%rowtype;

total number(4);

grade1 varchar2(10);

per1 number(6,2);

begin

open stu_c;

loop

fetch stu_c into s1;

exit when stu_c%notfound;

total:=s1.M1+s1.M2+s1.M3+s1.M4+s1.M5;

per1:=total/5;

if s1.M1<40 or s1.M2<40 or s1.M3<40 or s1.M4<40 or s1.M5<40 then

grade1:='fail';

elsif per1=40 then

grade1:='||| class';

elsif per1>40 and per1<60 then


Cycle-3(pl/sql)
grade1:='|| class';

elsif per1>59 then

grade1:='| class';

end if;

insert into abstract(stdno,stdname,class,intno,tot,grade,percentage)


values(s1.no,s1.name,s1.class,s1.intno,total,grade1,per1);

end loop;

close stu_c;

end;

PL/SQL procedure successfully completed.

SQL> select * from abstract;

STDNO STDNAME CLASS MONTH INTNO TOT GRADE PERCENTAGE DAT_ENTER

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

1 RAMESH MCA 1221 367 | class 73.4

2 RAJU MCA 1222 390 | class 78

3 DOLLY MCA 1223 384 | class 76.8

4 SURESH MCA 1224 388 | class 77.6

5 SMITH MCA 1225 394 | class 78.8

6 ALLEN MCA 1226 390 | class 78

6 rows selected.

6. Create an object to describe the details of address type data.

ANS. SQL> CREATE OR REPLACE TYPE address AS OBJECT

(house_no varchar2(10),

street varchar2(30),

city varchar2(20),

state varchar2(10),
Cycle-3(pl/sql)
pincode varchar2(10)

);

Type created.

DECLARE

residence address;

BEGIN

residence := address('103A', 'M.G.Road', 'Jaipur', 'Rajasthan','201301');

dbms_output.put_line('House No: '|| residence.house_no);

dbms_output.put_line('Street: '|| residence.street);

dbms_output.put_line('City: '|| residence.city);

dbms_output.put_line('State: '|| residence.state);

dbms_output.put_line('Pincode: '|| residence.pincode);

END;

PL/SQL procedure successfully completed.

SQL> SET SERVEROUTPUT ON;

SQL> /

House No: 103A

Street: M.G.Road

City: Jaipur

State: Rajasthan

Pincode: 201301

PL/SQL procedure successfully completed.


Cycle-3(pl/sql)
7. Develop Programs using BEFORE and AFTER Triggers, Row and Statement Triggers and INSTEAD OF
Triggers.

ANS.

After trigger:
CREATE TABLE emp3

EMP_ID number(10),

SALARY number(10),

EMP_NAME varchar2(50)

Table created.

INSERT INTO employee_salary VALUES (101,15000,'Pranav');

INSERT INTO employee_salary VALUES (201,40000,'Vikram');

INSERT INTO employee_salary VALUES (301,35000,'Nikhil');

CREATE OR REPLACE TRIGGER trg_log_EMP3

AFTER UPDATE

OF SALARY

ON emp3

FOR EACH ROW

WHEN ((NEW.SALARY - OLD.SALARY) > 50000)

DECLARE

username varchar2(20);

BEGIN

SELECT USER INTO username FROM dual;

INSERT INTO emp3_log VALUES (

:NEW.EMP_ID, :NEW.SALARY, :NEW.SALARY - :OLD.SALARY ,sysdate, username);

END;

Trigger created.
Cycle-3(pl/sql)
UPDATE employee_salary SET SALARY = '70000' WHERE emp_id = 101;

UPDATE employee_salary SET SALARY = '100000' WHERE emp_id = 301;

UPDATE employee_salary SET SALARY = '45000' WHERE emp_id = 201;

SELECT * FROM EMP3_LOG;

EMP_ID NEW_SALARY HIKE UPDATED_D UPDATED_BY

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

101 70000 55000 19-APR-22 MCA23

301 100000 65000 19-APR-22 MCA23

Instead of triggers:
CREATE TABLE employ

EMP_ID number(10),

SALARY number(10),

EMP_NAME varchar2(50)

CREATE TABLE dept2(

Dept_no NUMBER,

Dept_name VARCHAR2(50),

LOCATION VARCHAR2(50))

BEGIN

INSERT INTO DEPT2 VALUES(10,'HR','USA');

INSERT INTO DEPT2 VALUES(20,'SALES','UK');

INSERT INTO DEPT2 VALUES(30,'FINANCIAL','JAPAN');

COMMIT;

END;

/
Cycle-3(pl/sql)
PL/SQL procedure successfully completed.

BEGIN

INSERT INTO EMPLOY VALUES(1000,'XXX5',15000,'AAA',30);

INSERT INTO EMPLOY VALUES(1001,'YYY5',18000,'AAA',20) ;

INSERT INTO EMPLOY VALUES(1002,'ZZZ5',20000,'AAA',10);

COMMIT;

END;

CREATE VIEW emp3_view(

Employee_name,dept_name,location) AS

SELECT employ.emp_name,dept2.dept_name,dept2.location

FROM EMPLOY,dept2

WHERE employ.dept_no=dept2.dept_no

View created.

Sql>select * from employ_view;

EMPLOYEE_NAM DEPT_NAM LOCATIO


E E N
ZZZ HR USA
YYY SALES UK
XXX FINANCIAL JAPAN

CREATE TRIGGER emp5_trg

INSTEAD OF UPDATE

ON emp3_view

FOR EACH ROW

BEGIN
Cycle-3(pl/sql)
UPDATE dept2

SET location=:new.location

WHERE dept_name=:old.dept_name;

END;

//Trigger created.

BEGIN

UPDATE emp3_view SET location='FRANCE' WHERE employee_name='XXX5';

COMMIT;

END;

/ PL/SQL procedure successfully completed.

You might also like