SQL Assignment 5
SQL Assignment 5
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:
Output:
5. Write a PL/SQL procedure that checks whether a given number is even or
odd.
Solution:
DECLARE
N1 NUMBER := &NUM1;
BEGIN
IF MOD(N1,2) = 0 THEN
ELSE
END IF;
END;
Output:
6. Create a procedure that generates the Fibonacci series, and then invokes
the procedure to get the output.
Solution:
DECLARE
N NUMBER := 5;
I NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('SERIES:');
-- LOOP I = 2 TO N
FOR I IN 2..N
LOOP
TEMP:=FIRST+SECOND;
FIRST := SECOND;
SECOND := TEMP;
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;
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:
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:
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:
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
END;
/
Output:
18. Calculates factorial of number using function.
Solution:
DECLARE
num number;
factorial number;
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:
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:
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:
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:
Procedure created.
declare
begin
del_employee(1004);
dbms_output.put_line(' Delete employee Table ');
end;
/
Output: