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

SQL Assignment 5

The document contains solutions to various PL/SQL programming problems involving functions and procedures. Some key problems addressed include: 1) Writing functions to calculate the sum of natural numbers, find the minimum of two values, and square a number. 2) Creating procedures to find the greatest of three numbers, generate a Fibonacci series, and check if a number is prime. 3) Defining functions to return the net salary of an employee, count the total employees, and return the salary or city of a given customer/employee.

Uploaded by

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

SQL Assignment 5

The document contains solutions to various PL/SQL programming problems involving functions and procedures. Some key problems addressed include: 1) Writing functions to calculate the sum of natural numbers, find the minimum of two values, and square a number. 2) Creating procedures to find the greatest of three numbers, generate a Fibonacci series, and check if a number is prime. 3) Defining functions to return the net salary of an employee, count the total employees, and return the salary or city of a given customer/employee.

Uploaded by

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

ASSIGNMENT-5

1. Write a function to calculate the sum of 1st N natural numbers.

Solution:

DECLARE
X NUMBER;
N NUMBER;
I NUMBER;
--FUNCTION FOR FINDING SUM
FUNCTION FINDMAX(N IN NUMBER)
RETURN NUMBER
IS
SUMS NUMBER := 0;
BEGIN
--FOR LOOP FOR N TIMES ITERATION
FOR I IN 1..N
LOOP
SUMS := SUMS + I*(I+1)/2;
END LOOP;
RETURN SUMS;
END;
BEGIN
--DRIVER CODE
N := 4;
X := FINDMAX(N);
DBMS_OUTPUT.PUT_LINE('SUM: '|| X);
END;
--END OF PROGRAM
/

Output:
2. Finds the minimum of two values, procedure takes two numbers using IN
mode and returns their minimum using OUT parameters.

Solution:

DECLARE
A NUMBER;
B NUMBER;
C NUMBER;
PROCEDURE FINDMIN(X IN NUMBER, Y IN NUMBER, Z OUT NUMBER) IS
BEGIN
IF X < Y THEN
Z:= X;
ELSE
Z:= Y;
END IF;
END;
BEGIN
A:= 23;
B:= 45;
FINDMIN(A, B, C);
DBMS_OUTPUT.PUT_LINE(' MINIMUM OF (23, 45) : ' || C);
END;
/

Output:
3. This procedure computes the square of value of a passed value uses the
same parameter to accept a value and then return another result.

Solution:

DECLARE
A NUMBER;
PROCEDURE SQUARENUM(X IN OUT NUMBER) IS
BEGIN
X := X * X;
END;
BEGIN
A:= 23;
SQUARENUM(A);
DBMS_OUTPUT.PUT_LINE(' SQUARE OF (23): ' || A);
END;
/

Output:
4. Write a PL/SQL procedure that computes the maximum of three numbers
and then invokes the procedure from a PL/SQL block.

Solution:

--TO FIND THE GREATEST NUMBER


-- AMONG GIVEN THREE NUMBERS
DECLARE
--A ASSIGNING WITH 46
A NUMBER := 46;
--B ASSIGNING WITH 67
B NUMBER := 67;
--C ASSIGNING WITH 21
C NUMBER := 21;
BEGIN
--BLOCK START
--IF CONDITION START
IF A > B
AND A > C THEN
--IF A IS GREATER THEN PRINT A
DBMS_OUTPUT.PUT_LINE('GREATEST NUMBER IS '
||A);
ELSIF B > A
AND B > C THEN
--IF B IS GREATER THEN PRINT B
DBMS_OUTPUT.PUT_LINE('GREATEST NUMBER IS '
||B);
ELSE
--IF C IS GREATER THEN PRINT C
DBMS_OUTPUT.PUT_LINE('GREATEST NUMBER IS '
||C);
END IF;
--END IF CONDITION
END;
--END PROGRAM
/

Output:
5. Write a PL/SQL procedure that checks whether a given number is even or
odd.

Solution:

DECLARE

N1 NUMBER := &NUM1;

BEGIN

-- TEST IF THE NUMBER PROVIDED BY THE USER IS EVEN

IF MOD(N1,2) = 0 THEN

DBMS_OUTPUT.PUT_LINE ('THE NUMBER. '||N1||

' IS EVEN NUMBER');

ELSE

DBMS_OUTPUT.PUT_LINE ('THE NUMBER '||N1||' IS ODD NUMBER.');

END IF;

DBMS_OUTPUT.PUT_LINE ('DONE SUCCESSFULLY');

END;

Output:
6. Create a procedure that generates the Fibonacci series, and then invokes
the procedure to get the output.

Solution:

DECLARE

-- DECLARE VARIABLE FIRST = 0,


-- SECOND = 1 AND TEMP OF DATATYPE NUMBER
FIRST NUMBER := 0;
SECOND NUMBER := 1;
TEMP NUMBER;

N NUMBER := 5;
I NUMBER;

BEGIN

DBMS_OUTPUT.PUT_LINE('SERIES:');

--PRINT FIRST TWO TERM FIRST AND SECOND


DBMS_OUTPUT.PUT_LINE(FIRST);
DBMS_OUTPUT.PUT_LINE(SECOND);

-- LOOP I = 2 TO N
FOR I IN 2..N
LOOP
TEMP:=FIRST+SECOND;

FIRST := SECOND;
SECOND := TEMP;

--PRINT TERMS OF FIBONACCI SERIES


DBMS_OUTPUT.PUT_LINE(TEMP);
END LOOP;

END;
--PROGRAM END
/

Output:
7. Create a PL/SQL procedure that generates multiplication table up to a
given no.

Solution:

DECLARE
I NUMBER(2);
N NUMBER(2);
BEGIN
N:=&N;
FOR I IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE( N || ' * ' || I || ' = ' || N*I);
END LOOP;
END;

Output:
8. Create a PL/SQL procedure to check whether a given number is prime or
not.
Solution:
DECLARE
N NUMBER;
I NUMBER;
FLAG NUMBER;

BEGIN
I:=2;
FLAG:=1;
N:=&N;

FOR I IN 2..N/2
LOOP
IF MOD(N,I)=0
THEN
FLAG:=0;
EXIT;
END IF;
END LOOP;

IF FLAG=1
THEN
DBMS_OUTPUT.PUT_LINE('PRIME');
ELSE
DBMS_OUTPUT.PUT_LINE('NOT PRIME');
END IF;
END;
/

Output:
9. Create a procedure that will take a number as input and will give reverse
of the number as output.

Solution:

DECLARE
NUM NUMBER;
REVERSE_NUM NUMBER:=0;

BEGIN
NUM:=98765;
WHILE NUM>0
LOOP
REVERSE_NUM:=(REVERSE_NUM*10) + MOD(NUM,10);
NUM:=TRUNC(NUM/10);
END LOOP;

DBMS_OUTPUT.PUT_LINE(' REVERSED NUMBER IS : '|| REVERSE_NUM);


END;
/

Output:
10. Write a procedure that checks whether a year given by user leap year or
not.

Solution:

DECLARE
year NUMBER := 2012;
BEGIN
IF MOD(year, 4)=0
AND
MOD(year, 100)!=0
OR
MOD(year, 400)=0 THEN
dbms_output.Put_line(year || ' is leap year ');
ELSE
dbms_output.Put_line(year || ' is not leap year.');
END IF;
END;
/

Output:
11. Create a procedure to check whether a given number is Armstrong or
Not.

Solution:

DECLARE
N NUMBER(3);
S NUMBER(3):=0;
T NUMBER(3);
BEGIN
N:=&N;
T:=N;
WHILE T>0 LOOP
S:=S+POWER((T MOD 10),3);
T:=TRUNC(T/10);
END LOOP;
IF(S=N) THEN
DBMS_OUTPUT.PUT_LINE('THE GIVEN NUMBER ' || N || ' IS AN ARMSTRONG
NUMBER');
ELSE
DBMS_OUTPUT.PUT_LINE('THE GIVEN NUMBER ' || N || ' IS NOT AN ARMSTRONG
NUMBER');
END IF;
END;
/

Output:
12. Write a procedure that swaps two numbers given by user.

Solution:

DECLARE
A NUMBER;
B NUMBER;
TEMP NUMBER;
BEGIN
A:=5;
B:=10;
DBMS_OUTPUT.PUT_LINE('BEFORE SWAPPING:');
DBMS_OUTPUT.PUT_LINE('A='||A||' B='||B); TEMP:=A;
A:=B;
B:=TEMP;
DBMS_OUTPUT.PUT_LINE('AFTER SWAPPING:');
DBMS_OUTPUT.PUT_LINE('A='||A||' B='||B);
END;
/

Output:
13. Write a PL/SQL function to return the net salary for a given the employee
number.

Table create:

Solution:
CREATE OR REPLACE FUNCTION NETSAL(N IN NUMBER)
RETURN NUMBER
IS
NET_SALARY NUMBER(5):=0;
BEGIN
SELECT NET_SALARY INTO NET_SALARY
FROM EMPLOYEE_INFO
WHERE ENO=N;
RETURN NET_SALARY;
END;
/
DECLARE
N NUMBER:=&EMPLOYEE_NUMBER;
NET_SALARY NUMBER;
BEGIN
NET_SALARY:=NETSAL(N);
DBMS_OUTPUT.PUT_LINE('NET SALARY: '||NET_SALARY);
END;
/

Output:
14. Create a function that will return total no. of employee in the emp table
and then execute the function.

Table create:

Solution:

CREATE OR REPLACE FUNCTION TOTAL_EMPLOYEES


RETURN NUMBER IS
TOTAL NUMBER(2) := 0;
BEGIN
SELECT COUNT(*) INTO TOTAL
FROM EMPLOYEE;

RETURN TOTAL;
END;
/
DECLARE
C NUMBER(2);
BEGIN
C := TOTAL_EMPLOYEES();
DBMS_OUTPUT.PUT_LINE('TOTAL NO. OF EMPLOYEES: ' || C);
END;
/

Output:
15. Table : Customer(cno, name, city, salary, dtbirth). Write a function that
takes as input a customer number and returns the city in which the
customer lives.

Table creation:

Solution:

create or replace function Cust_city(E in number) return varchar2


is
c_city varchar2(20);
begin
select city into c_city from customer where cno=E;
return c_city;
end;
/

Function created.
declare
c varchar2(20):=0;
i number;
begin
i:=&i;
c:= Cust_city(i)
dbms_output.put_line(' customer City is: ' || c);
end;
/
Output:
16. Create a function that takes as input a employee no. and returns the
salary of the employee(use employee table).

Table creation:

Solution:

create or replace function salary_of_emp(E number) return number


is
salary_emp number(5):=0;
begin
select salary into salary_emp from employee where empid=E;
return salary_emp;
end;
/

declare
c number:=0;
temp number;
begin
temp:=&temp;
c:= salary_of_emp(temp);
dbms_output.put_line(' Net salary of the employee is: ' || c);
end;
/

Output:
17. Define, invoke a simple PL/SQL function that computes and returns the
maximum of three numbers.

Solution:

DECLARE

A NUMBER := 46;

B NUMBER := 67;

C NUMBER := 21;
BEGIN

IF A > B
AND A > C THEN

DBMS_OUTPUT.PUT_LINE('GREATEST NUMBER IS '


||A);
ELSIF B > A
AND B > C THEN

DBMS_OUTPUT.PUT_LINE('GREATEST NUMBER IS '


||B);
ELSE

DBMS_OUTPUT.PUT_LINE('GREATEST NUMBER IS '


||C);
END IF;

END;
/

Output:
18. Calculates factorial of number using function.

Solution:

DECLARE
num number;
factorial number;

FUNCTION fact(x number)


RETURN number
IS
f number;
BEGIN
IF x=0 THEN
f := 1;
ELSE
f := x * fact(x-1);
END IF;
RETURN f;
END;

BEGIN
num:= 6;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' || factorial);
END;
/
Output:
19. Create a function that will return the sum of the expression 1*1 + 2*2 +3*3
+ - - - - - - - - - +N*N

Solution:

create or replace function sum_of_s(n in number) return number


is
i number;
sums number:=0;
begin
for i in 1..n
loop
sums:= sums +(i*i);
end loop;
return sums;
end;
/
Function created.

declare
c number:=0;
d number;
begin
d:=&d;
c:=sum_of_s(d);
dbms_output.put_line(' Sum od series is: ' || c);
end;
/
Output:
20. Create a function that will return total salary of employee table.

Table creation:

Solution:

create or replace function total_sal return number


is
t_sal number:=0;
begin
select sum(salary) into t_sal from employee;
return t_sal;
end;
/

Function created.

declare
c number:=0;
begin
c:=total_sal();
dbms_output.put_line(' Total Salary of the employee Table: ' || c);
end;
/

Output:
21. Write a function that accepts an employee number and check its salary. If
it is less than 2000 then increment it by 20% of its salary.

Table creation:

Solution:

create or replace function inc_sal(E in number) return number


is
t_sal number:=0;
temp number:=0;
begin
select salary into t_sal from employee where empid=E;
if t_sal<50000 then
temp:= t_sal+(t_sal*.2);
else
dbms_output.put_line(' Salary greater than 20000');
end if;
return temp;
end;
/

Function created.

declare
c number:=0;
i number;
begin
i:=&i;
c:=inc_sal(i);
if c>0 then
dbms_output.put_line(' New Salary of the employee is: ' || c);
else
dbms_output.put_line(' Already salary is high no increment: ');
end if;
end;
/
Output:
22. Write a procedure del_employee to delete an employee on the employee
number. Write a code to call this procedure.
Table creation:

Solution:

create or replace procedure del_employee(del in number)


is
begin
delete from employee where empid=del;
end;
/

Procedure created.

declare
begin
del_employee(1004);
dbms_output.put_line(' Delete employee Table ');
end;
/
Output:

You might also like