Вы находитесь на странице: 1из 9

EXNO:14 PL/SQL code to display employee details

SQL> create table empp(emp_no number(4),emp_name varchar2(30),street varchar2(20),city varchar2(20));

Table created.

SQL> insert into empp values(&emp_no,'&emp_name','&street','&city');


Enter value for emp_no: 1000
Enter value for emp_name: rajesh
Enter value for street: first cross
Enter value for city: vellore
old 1: insert into empp values(&emp_no,'&emp_name','&street','&city')
new 1: insert into empp values(1000,'rajesh','first cross','vellore')

1 row created.

SQL> /
Enter value for emp_no: 1001
Enter value for emp_name: paramesh
Enter value for street: second cross
Enter value for city: vellore
old 1: insert into empp values(&emp_no,'&emp_name','&street','&city')
new 1: insert into empp values(1001,'paramesh','second cross','vellore')

1 row created.

SQL> /
Enter value for emp_no: 1002
Enter value for emp_name: prasad
Enter value for street: third cross
Enter value for city: plk
old 1: insert into empp values(&emp_no,'&emp_name','&street','&city')
new 1: insert into empp values(1002,'prasad','third cross','plk')

1 row created.

SQL> /
Enter value for emp_no: 1003
Enter value for emp_name: vijay
Enter value for street: shivaji nagar
Enter value for city: banglore
old 1: insert into empp values(&emp_no,'&emp_name','&street','&city')
new 1: insert into empp values(1003,'vijay','shivaji nagar','banglore')

1 row created.

SQL> /
Enter value for emp_no: 1004
Enter value for emp_name: karthick
Enter value for street: Mgr nagar
Enter value for city: chennai

4
old 1: insert into empp values(&emp_no,'&emp_name','&street','&city')
new 1: insert into empp values(1004,'karthick','Mgr nagar','chennai')

1 row created.

SQL> select * from empp;

EMP_NO EMP_NAME STREET CITY


---------- ------------------------------ -------------------- --------------------
1000 rajesh first cross vellore
1001 paramesh second cross vellore
1002 prasad third cross plk
1003 vijay shivaji nagar banglore
1004 karthick Mgr nagar chennai

SQL> set serveroutput on


SQL> declare
2 eno empp.emp_no%type;
3 ename empp.emp_name%type;
4 begin
5 eno:=&eno;
6 select emp_no,emp_name into eno,ename from empp where emp_no=eno;
7 dbms_output.put_line('----------------------OUTPUT----------------------');
8 dbms_output.put_line('Employee No:'||eno);
9 dbms_output.put_line('Employee Name:'||ename);
10 end;
11 /

Enter value for eno: 1000


old 5: eno:=&eno;
new 5: eno:=1000;
----------------------OUTPUT----------------------
Employee No:1000
Employee Name:rajesh

PL/SQL procedure successfully completed.


SQL> /
Enter value for eno: 1004
old 5: eno:=&eno;
new 5: eno:=1004;
----------------------OUTPUT----------------------
Employee No:1004
Employee Name:karthick

PL/SQL procedure successfully completed.

4
EXNO-15 EB BILL

SQL>create table eb_bill(name varchar2(10), e_id number(5), pre_reading number(5),curr_reading number(5));

Table created.
SQL>insert into eb_bill values ('&name', &e_id, &pre_reading, &curr_reading);

SQL> select *from eb_bill;

NAME E_ID PRE_READING CURR_READING


---------- ---------- ----------- ------------ ----------- ------------
sudan 1001 103 250
Jana 1002 500 750
kishore 1003 430 690
Arun 1004 340 400
Kumaran 1005 750 1050

SQL> set serveroutput on


SQL> declare
2 e_name varchar2(10);
3 start_id number(5):=&start_id;
4 end_id number(5):=&end_id;
5 e_pre_reading number(5);
6 e_curr_reading number(5);
7 units number(5);
8 amount number(5);
9 x number;
10 begin
11 for x in start_id..end_id loop
12 select name,pre_reading,curr_reading into e_name,e_pre_reading,e_curr_reading from eb_bill where
e_id=x;
13 units:=e_curr_reading-e_pre_reading;
14 if units>='100' then
15 amount:=units*1.75;
16 end if;
17 if (units>'100' and units<='200') then
18 amount:=units*3;
19 end if;
20 if(units>'200') then
21 amount:=units*4;
22 end if;
23 dbms_output.put_line('Name:'||e_name||'ID:'||x||'Units:'||units||'Amount:'||amount);
24 end loop;
25 end;
26 /
Enter value for start_id: 1001
old 3: start_id number(5):=&start_id;
new 3: start_id number(5):=1001;
Enter value for end_id: 1005
old 4: end_id number(5):=&end_id;

4
new 4: end_id number(5):=1005;
Name:sudanID:1001Units:147Amount:441
Name:JanaID:1002Units:250Amount:1000
Name:kishoreID:1003Units:260Amount:1040
Name:ArunID:1004Units:60Amount:1040
Name:KumaranID:1005Units:300Amount:1200

PL/SQL procedure successfully completed.

EXNO-16 PL/SQL Block to handle Built-In function


SQL> create table alumni(name varchar2(10), address varchar2(10), degree varchar2(10), batch
number(10));

Table created.

SQL> insert into alumni values('&name','&address','&degree',&batch);


Enter value for name: babu
Enter value for address: pcm
Enter value for degree: BE
Enter value for batch: 2002
old 1: insert into alumni values('&name','&address','&degree',&batch)
new 1: insert into alumni values('babu','pcm','BE',2002)

1 row created.

SQL> /
Enter value for name: christy
Enter value for address: kerela
Enter value for degree: MCA
Enter value for batch: 2000
old 1: insert into alumni values('&name','&address','&degree',&batch)
new 1: insert into alumni values('christy','kerela','MCA',2000)

1 row created.

SQL> /
Enter value for name: Malar
Enter value for address: chennai
Enter value for degree: Mphil
Enter value for batch: 2003
old 1: insert into alumni values('&name','&address','&degree',&batch)
new 1: insert into alumni values('Malar','chennai','Mphil',2003)

1 row created.

SQL> select *from alumni;

NAME ADDRESS DEGREE BATCH


---------- ---------- ---------- ---------- ---------- ----------
babu pcm BE 2002

4
christy kerela MCA 2000
Malar chennai Mphil 2003

To handle built-in exception like NO_DATA_FOUND, TOO_MANY_ROWS.

SQL> set serveroutput on


SQL> declare
2 a_batch number(10);
3 a_name varchar2(10);
4 ad varchar2(10);
5 a_degree varchar2(10);
6 begin
7 a_batch:=&batch;
8 select name, address, degree into a_name, ad, a_degree from alumni where batch= a_batch;
9 dbms_output.put_line('Alumni_Name: ' || a_name || ' Batch : ' || a_batch );
10 exception
11 when no_data_found then
12 dbms_output.put_line('The ' || a_batch || ' Record Not Found. Please Insert.');
13 when too_many_rows then
14 dbms_output.put_line('The ' || a_batch ||' Occurs Two Times. Please Select One Row Record.');
15 end;
16 /

Enter value for batch: 2003


old 7: a_batch:=&batch;
new 7: a_batch:=2003;
Alumni_Name: Malar Batch : 2003

PL/SQL procedure successfully completed.

SQL> /
Enter value for batch: 2004
old 7: a_batch:=&batch;
new 7: a_batch:=2004;
The 2004 Record Not Found. Please Insert.

PL/SQL procedure successfully completed.

EXNO:17 USER DEFINED EXCEPTION

SQL> set serveroutput on


SQL> declare
2 a number(3);
3 b number(3);
4 c number(3);
5 divide_error exception;
6 begin
7 a:=&a;
8 b:=&b;
9 if(b=0)then
10 raise divide_error;
11 end if;

4
12 c:=a/b;
13 dbms_output.put_line('The value of '||a||'/'||b||'is'||c);
14 exception
15 when divide_error then
16 dbms_output.put_line('A Number cannot be divide by Zero');
17 end;
18 /
Enter value for a: 10
old 7: a:=&a;
new 7: a:=10;
Enter value for b: 2
old 8: b:=&b;
new 8: b:=2;
The value of 10/2is5

PL/SQL procedure successfully completed.

SQL> /
Enter value for a: 5
old 7: a:=&a;
new 7: a:=5;
Enter value for b: 0
old 8: b:=&b;
new 8: b:=0;
A Number cannot be divide by Zero

PL/SQL procedure successfully completed.

EXNO: 18 PROCEDURE & FUNCTION USING PL/SQL


A.) PL/SQL USING PROCEDURES

Aim:
To write a program in PL/SQL using procedure and functions.

PROCEDURE:
SQL> create table pay(eno number(4),name varchar2(25),bpay number(5),hra number(3),da number(3),lic
number(3),npay number(5));

Table created.

SQL> insert into pay values(&eno,'&name',&bpay,&hra,&da,&lic,&npay);


Enter value for eno: 1001
Enter value for name: kumar
Enter value for bpay: 6000
Enter value for hra: 60
Enter value for da: 40
Enter value for lic: 30
Enter value for npay: 0
old 1: insert into pay values(&eno,'&name',&bpay,&hra,&da,&lic,&npay)
new 1: insert into pay values(1001,'kumar',6000,60,40,30,0)

4
1 row created.
SQL> /
Enter value for eno: 1002
Enter value for name: prabhu
Enter value for bpay: 5500
Enter value for hra: 55
Enter value for da: 35
Enter value for lic: 25
Enter value for npay: 0
old 1: insert into pay values(&eno,'&name',&bpay,&hra,&da,&lic,&npay)
new 1: insert into pay values(1002,'prabhu',5500,55,35,25,0)
1 row created.
SQL> /
Enter value for eno: 1003
Enter value for name: stalin
Enter value for bpay: 5000
Enter value for hra: 50
Enter value for da: 30
Enter value for lic: 20
Enter value for npay: 0
old 1: insert into pay values(&eno,'&name',&bpay,&hra,&da,&lic,&npay)
new 1: insert into pay values(1003,'stalin',5000,50,30,20,0)
1 row created.
SQL> /
Enter value for eno: 1004
Enter value for name: parthi
Enter value for bpay: 7000
Enter value for hra: 70
Enter value for da: 45
Enter value for lic: 40
Enter value for npay: 0
old 1: insert into pay values(&eno,'&name',&bpay,&hra,&da,&lic,&npay)
new 1: insert into pay values(1004,'parthi',7000,70,45,40,0)
1 row created.

SQL> select *from pay;


ENO NAME BPAY HRA DA LIC NPAY
1001 kumar 6000 60 40 30 0
1002 prabhu 5500 55 35 25 0
1003 stalin 5000 50 30 20 0
1004 parthi 7000 70 45 40 0

SQL> create or replace procedure calc(en in number) is


2 begin
3 update pay set npay=bpay+hra+da-lic;
4 exception
5 when no_data_found then
6 dbms_output.put_line('Employee number doesnot Exist');
7 end;
8 /
Procedure created.
SQL> exec calc(1001);

4
PL/SQL procedure successfully completed.
SQL> exec calc(1002);
PL/SQL procedure successfully completed.
SQL> exec calc(1003);
PL/SQL procedure successfully completed.
SQL> exec calc(1004);
PL/SQL procedure successfully completed.

SQL> select *from pay;


ENO NAME BPAY HRA DA LIC NPAY
---------- ------------------------- ---------- ---------- ---------- ---------- ---------- ----------
1001 kumar 6000 60 40 30 6070
1002 prabhu 5500 55 35 25 5565
1003 stalin 5000 50 30 20 5060
1004 parthi 7000 70 45 40 7075
B.PL/SQL USING FUNCTIONS

Find the given input is character or digit using functions


SQL> create or replace function is_digit(inchar varchar2)return boolean is
2 begin
3 if(substr(inchar,1,1)in('0','1','2','3','4','5','6','7','8','9'))
4 then
5 return true;
6 else
7 return false;
8 end if;
9 exception
10 when others then
11 return false;
12 end;
13 /
Function created.

SQL> set serveroutput on


SQL> declare
2 ch char:='&in';
3 begin
4 if is_digit(ch)=true
5 then
6 dbms_output.put_line('Is a Digit');
7 else
8 dbms_output.put_line('Is a character');
9 end if;
10 end;
11 /

Enter value for in: a


old 2: ch char:='&in';
new 2: ch char:='a';
Is a character
PL/SQL procedure successfully completed.

4
SQL> /
Enter value for in: 1
old 2: ch char:='&in';
new 2: ch char:='1';
Is a Digit
PL/SQL procedure successfully completed.
Find factorial using functions

SQL> create or replace function fac(n integer)return integer is


2 f number;
3 begin
4 f:=1;
5 for i in 1..n
6 loop
7 f:=f*i;
8 end loop;
9 return f;
10 end;
11 /

Function created.

SQL> select fac(5) from dual;

FAC(5)
----------
120

Вам также может понравиться