0% found this document useful (0 votes)
136 views

Experiment 10

The document describes experiments conducted in SQL to: 1. Create a table to store student records and write a procedure to calculate total marks by passing student details as arguments 2. Write a procedure to display the multiplication of a number from 1 to a given number 3. Write a procedure to increase employee salary by passing employee number and increase amount 4. Write a function to check if a number is divisible by another number 5. Write a function to calculate power of a number 6. Write a function to check if a number is even or odd

Uploaded by

sarjitgaur
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)
136 views

Experiment 10

The document describes experiments conducted in SQL to: 1. Create a table to store student records and write a procedure to calculate total marks by passing student details as arguments 2. Write a procedure to display the multiplication of a number from 1 to a given number 3. Write a procedure to increase employee salary by passing employee number and increase amount 4. Write a function to check if a number is divisible by another number 5. Write a function to calculate power of a number 6. Write a function to check if a number is even or odd

Uploaded by

sarjitgaur
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/ 12

EXPERIMENT 10

Q1: Write a procedure to calculate total for the all


the students and pass regno, mark1, &
mark2 as arguments.

SQL> create table itstudent2(regno number(3),name


varchar(9),mark1 number(3),mark2 number(3));
Table created.
SQL> insert into itstudent2 values(&a,'&b',&c,&d);
Enter value for a: 110
Enter value for b: Arun
Enter value for c: 99
Enter value for d: 100
old 1: insert into itstudent2 values(&a,'&b',&c,&d)
new 1: insert into itstudent2
values(110,'Arun',99,100)

1 row created.

SQL> /
Enter value for a: 112
Enter value for b: Siva
Enter value for c: 99
Enter value for d: 90
old 1: insert into itstudent2 values(&a,'&b',&c,&d)
new 1: insert into itstudent2
values(112,'Siva',99,90)
1 row created.

SQL> select * from itstudent2;

REGNO NAME MARK1 MARK2


---------- --------- ---------- ----------
110 Arun 99 100
112 Siva 99 90

SQL> alter table itstudent2 add(total number(5));

Table altered.

SQL> select * from itstudent2;

REGNO NAME MARK1 MARK2 TOTAL


---------- --------- ---------- ---------- ----------
110 Arun 99 100
112 Siva 99 90
Q2: Write a PL/SQL procedure called MULTI_TABLE that
takes two numbers as parameter and
displays the multiplication of the first parameter till
the second parameter.

SQL> create or replace procedure p1(sno number,mark1


number,mark2 number) is
2 tot number(5);
3 begin
4 tot:=mark1+mark2;
5 update itstudent2 set total=tot where regno=sno;
6 end;
7 /

Procedure created.

SQL> declare
2 cursor c1 is select * from itstudent2;
3 rec itstudent2 % rowtype;
4 begin
5 open c1;
6 loop
7 fetch c1 into rec;
8 exit when c1%notfound;
9 p1(rec.regno,rec.mark1,rec.mark2);
10 end loop;
11 close c1;
12 end;
13 /

PL/SQL procedure successfully completed.

SQL> select * from itstudent2;


REGNO NAME MARK1 MARK2 TOTAL
---------- --------- ---------- ---------- ----------
110 Arun 99 100 199
112 Siva 99 90 189

SQL> create or replace procedure p1(sno number,mark1


number,mark2 number) is
2 2 tot number(5);
3 3 begin
4 4 tot:=mark1+mark2;
5 5 update itstudent2 set total=tot where regno=sno;
6 end;
7 /

Warning: Procedure created with compilation errors.

SQL> create or replace procedure p1(sno number,mark1


number,mark2 number) is
2 tot number(5);
3 begin
4 tot:=mark1+mark2;
5 update itstudent2 set total=tot where regno=sno;
6 end;
7 /
Procedure created.

SQL> declare
2 cursor c1 is select * from itstudent2;
3 rec itstudent2 % rowtype;
4 begin
5 open c1;
6 loop
7 fetch c1 into rec;
8 exit when c1%notfound;
9 p1(rec.regno,rec.mark1,rec.mark2);
10 end loop;
11 close c1;
12 end;
13 /

PL/SQL procedure successfully completed.

SQL> select * from itstudent2;

REGNO NAME MARK1 MARK2 TOTAL


---------- --------- ---------- ---------- ----------
110 Arun 99 100 199
112 Siva 99 90 189
Q2: Write a PL/SQL procedure called MULTI_TABLE that
takes two numbers as parameter and
displays the multiplication of the first parameter till
the second parameter.
SQL> create or replace procedure multi_table (a number,
b number) as mul number;
Begin
for i in 1..b loop
mul := a * i;
dbms_output.put_line (a || 'x' || i || '=' || mul);
end loop;
end;
/

Procedure created.

SQL> declare
a number;
b number;
begin
a := 4; b := 3;
multi_table(a,b);
end;
/

PL/SQL procedure successfully completed.


Enter value for a: 4
Enter value for b: 3
old 1: declare a number; b number; begin a := &a;
b :=&b; multi_table(a,b); en
d;
new 1: declare a number; b number; begin a := 4; b
:=3; multi_table(a,b); end;
4*1=4
4*2=8
4*3=12

PL/SQL procedure successfully completed.

Q3: Consider the EMPLOYEE (EMPNO, SALARY, ENAME) Table.


Write a procedure raise_sal which increases the salary
of an employee. It accepts an employee
number and salary increase amount. It uses the employee
number to find the current salary from
the EMPLOYEE table and update the salary.
SQL> create table emp(EMPNO number(2), ENAME
varchar(10), JOB varchar(5), DEPTNO number(2), SAL
number(6));
insert into emp values(1, 'Mathi', 'AP', 1, 10000);
select * from emp;
insert into emp values(2, 'Arjun', 'ASP', 2, 15000);
insert into emp values(3, 'Gugan', 'ASP', 1, 15000);
insert into emp values(4, 'Karthik', 'Prof', 2, 30000);
insert into emp values(5, 'Akalya', 'AP', 1, 10000);
select * from emp order by empno;
select * from emp;
create or replace procedure raise_sal( mempno emp.empno
% type, msal_percent number ) as
begin
update emp set sal = sal + sal*msal_percent /100 where
empno = mempno;
end;
declare
cursor c1 is select * from emp;
rec emp % rowtype;
begin
open c1;
loop
fetch c1 into rec;
exit when c1%notfound;
raise_sal(rec.empno,10);
end loop;
close c1;
end;

SQL> select * from emp;

EMPNO ENAME JOB DEPTNO SAL


---------- ---------- ----- ---------- ----------
1 Mathi AP 1 11000
4 Karthik Prof 2 33000
2 Arjun ASP 2 16500
3 Gugan ASP 1 16500
5 Akalya AP 1 11000

Q4: Write a PL/SQL function CheckDiv that takes two


numbers as arguments and returns the
values 1 if the first argument passed to it is
divisible by the second argument, else will return the
value 0;
SQL> create or replace function checkdiv (n1 number, n2
number) return number as
res
2 number;
3 begin
4 if mod (n1, n2) = 0 then
5 res := 1;
6 else
7 res:= 0;
8 end if;
9 return res;
10 end;
11 /

Function created.

SQL> set serveroutput on;


SQL> declare
2 a number;
3 b number;
4 begin
5 a:=&a; b:=&b;
6 dbms_output.put_line('Result is='||checkdiv(a,b));
7 end;
8 /
Enter value for a: 4
Enter value for b: 2
old 5: a:=&a; b:=&b;
new 5: a:=4; b:=2;
Result is=1

PL/SQL procedure successfully completed.

Q5: Write a PL/SQL function called POW that takes two


numbers as argument and return the
value of the first number raised to the power of the
second .

SQL> create or replace function pow (n1 number, n2


number) return number as
2 res number;
3 begin
4 select power ( n1, n2) into res from dual; return
res;
5 end;
6 /
SQL>set serveroutput on;
SQL>declare
a number;
b number;
begin
a:=&a; b:=&b;
dbms_output.put_line('power(n1,n2)='||pow(a,b));
end;
/
Enter value for a: 2
old 5: a:=&a;
new 5: a:=2;
Enter value for b: 3
old 6: b:=&b;
new 6: b:=3;
power(n1,n2)=8

Q6: Write a PL/SQL function ODDEVEN to return value


TRUE if the number passed to it is
EVEN else will return FALSE.

SQL> create or replace function oddeven (n number)


return boolean as
begin
if mod (n, 2) = 0 then return true;
else
return false;
end if;
end;
/
SQL>declare
a number; b boolean;
begin
a:=&a; b:=oddeven(a);
if b then
dbms_output.put_line('The given number is Even');
else
dbms_output.put_line('The given number is Odd');
end if;
end;
/
Enter value for a: 5
old 5: a:=&a; new 5: a:=5;
The given number is Odd

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy