Beruflich Dokumente
Kultur Dokumente
-------------------------------------------------------------------------------SQL> Declare
2 a number(3):=5;
3 b number(3):=6;
4 c number(3);
5 Begin
6 c:=a+b;
7 dbms_output.put_line (The value of c is||c);
8 end;
9 /
OUTPUT:PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> /
The value of c is 11
3Q) Create a PL/SQL program that prints even and odd numbers between 1 to 50
--------------------------------------------------------------------------------------------------------SQL> declare
2 begin
3 dbms_output.put_line('Even Numbers between 1 and 50');
4 for i in 1..50
5 loop
6 if ( i mod 2 = 0 ) then
7 dbms_output.put_line(i);
8 end if;
9 end loop;
10 dbms_output.put_line('Odd Numbers between 1 and 50');
11 for i in 1..50
12 loop
13 if ( i mod 2 <>0) then
14 dbms_output.put_line(i);
15 end if;
16 end loop;
17 end;
18 /
OUTPUT:Even Numbers between 1 and 50
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
Odd Numbers between 1 and 50
1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
PL/SQL procedure successfully completed.
4Q) Create a PL/SQL program that checks whether a given no is Prime or Not.
----------------------------------------------------------------------------------------------------SQL> declare
2 n number(3):=&n;
3 i number(3):=0;
4 count1 number(3):=0;
5 begin
6 while i<=n
7 loop
8 if n mod i=0 then
9 count1:=count1+1;
10 end if;
11 i:=i+1;
12 end loop;
13 if count1=2 then
14 dbms_output.put_line('It is prime no');
15 else
16 dbms_output.put_line('It is not prime no');
17 end if;
18 end;
19 /
OUTPUT:Enter value for n: 3
old 2: n number(3):=&n;
new 2: n number(3):=3;
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> /
Enter value for n: 3
old 2: n number(3):=&n;
new 2: n number(3):=3;
It is prime no
PL/SQL procedure successfully completed.
SQL> /
Enter value for n: 4
old 2: n number(3):=&n;
new 2: n number(3):=4;
It is not prime no
PL/SQL procedure successfully completed.
5Q)Write PL/SQL program to print prime numbers between 1-10
------------------------------------------------------------------------------------SQL> declare
2 n number(3):=&n;
3 c number(3);
4 begin
5 for i IN 1..n
6 loop
7 c:=0;
8 for j IN 1..i
9 loop
10 if i mod j=0 then
11 c:=c+1;
12 end if;
13 end loop;
14 if c=2 then
15 dbms_output.put_line(i);
16 end if;
17 end loop;
18* end;
SQL> /
OUTPUT:Enter value for n: 10
old 2: n number(3):=&n;
new 2: n number(3):=10;
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> /
Enter value for n: 10
old 2: n number(3):=&n;
new 2: n number(3):=10;
2
3
5
7
PL/SQL procedure successfully completed.
55
89
144
233
377
610
987
7)Write PL/SQL program to find factorial of a given number
------------------------------------------------------------------------------SQL> declare
2 i number(3);
3 n number(3):=&n;
4 Fact number(5):=1;
5 begin
6 for i IN 1..n
7 loop
8 fact:=fact*i;
9 end loop;
10 dbms_output.put_line('Fact of '||n||'='||Fact);
11* end;
12 /
OUTPUT:SQL>Enter value for n: 4
old 3: n number(3):=&n;
new 3: n number(3):=4;
Fact of 4=24
PL/SQL procedure successfully completed.
sum of marks:270
Avg of marks:90
I division
PL/SQL procedure successfully completed.
SQL> /
Enter value for no: 55
old 4: no std.sno%type:=&no;
new 4: no std.sno%type:=55;
No record found
PL/SQL procedure successfully completed.
9Q)Calculate area and circumference of a circle (radius 1-7) and insert area, radius,
circumference into new table called area
----------------------------------------------------------------------------------------------------------SQL> create table area(radius number(5),area number(5,2),circumference number(5,2));
Table created.
SQL> desc area
Name
Null? Type
----------------------------------------- -------- ---------------------------RADIUS
NUMBER(5)
AREA
NUMBER(5,2)
CIRCUMFERENCE
NUMBER(5,2)
SQL> declare
2 i number(5);
3 area number(5,2);
4 circum number(5,2);
5 pi number(5,2):=3.14;
6 begin
7 for i in 1..7
8 loop
9 area:=pi*i*i;
10 circum:=2*pi*i;
11 insert into area values(i,area,circum);
12 end loop;
13* end;
SQL> /
OUTPUT:PL/SQL procedure successfully completed.
SQL> select * from area;
RADIUS
AREA CIRCUMFERENCE
120
PL/SQL procedure successfully completed.
22Q)Write a package specification and package body which consists procedure and
function.
--------------------------------------------------------------------------------------------------------SQL> create or replace package p1 is
2 function f1(a in number,b in number)return number;
3 procedure pp(x in number,y in number,z out number);
4 end p1;
5 /
Package created.
SQL> create or replace package body p1 is
2 function f1(a in number,b in number)return number is
3 c number;
4 begin
5 c:=a+b;
6 return c;
7 end f1;
8 procedure pp(x in number,y in number,z out number) is
9 begin
10 z:=x*y;
11 end pp;
12 end p1;
13 /
Package body created.
SQL> select p1.f1(5,6) from dual;
P1.F1(5,6)
---------11
SQL> declare
2 b number;
3 begin
4 b:=p1.f1(3,4);
5 dbms_output.put_line(b);
6 p1.pp(4,5,b);
7 dbms_output.put_line(b);
8 end;
9 /
OUTPUT:7
20
PL/SQL procedure successfully completed.
23Q)Write a Program which uses cursors to get student info and displays the same.
--------------------------------------------------------------------------------------------------------SQL> declare
2 cursor
3 c1 is select * from std;
4 std_rec std%rowtype;
5 begin
6 open c1;
7 loop
8 fetch c1 into std_rec;
9 dbms_output.put_line(std_rec.sno||' '||std_rec.sname||' '||std_rec.smarks);
10
11
12
13
OUTPUT:585 George 90
586 Jenny 95
587 Andy 98
584 Steven 94
24Q)Create a PL/SQL program which uses a cursor to get emp info then print emp
info if the salary>50000.
--------------------------------------------------------------------------------------------------------SQL> declare
2 emp_rec emp%rowtype;
3 cursor emp_cur is
4 select eno,ename,esal from emp where esal>50000;
5 begin
6 for emp_rec in emp_cur loop
7 dbms_output.put_line(emp_rec.eno||' '||emp_rec.ename||' '||emp_rec.esal);
8 end loop;
9 end;
10 /
OUTPUT:ray 55000
ria 52000
PL/SQL procedure successfully completed.
1 abc 200
2 bcd 3
3 fgh 453
45 fhdg 563
7 ret 543
8 ded 233
3c
4a
5d
6e
7k
70
65
75
80
67
60
66
67
67
87
60
70
72
75
76
190
201
214
222
7 rows selected.
SQL> delete from student where sname='a';
2 rows deleted.
SQL> select * from del;
TABLENAME OPERATION
MODIFIEDDATE
-------------------- --------------------------- --------Student
Delete
25-APR-10
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
PL/SQL procedure successfully completed.
29Q)Creation of VIEWS.
--------------------------------SQL> create view stud(sno,sname,total) as select sno,sname,total from student;
View created.
SQL> select * from stud;
SNO SNAME TOTAL
-------- ---------------- ---------1a
210
2b
190
3c
190
4a
201
5d
214
6e
222
6 rows selected.