You are on page 1of 7

QUESTION 1:-

DISPLAY YOUR NAME 5 TIMES USING FOR LOOP.


QUERY:-
declare
v_ename varchar2(200);
begin
v_ename:=’SUBHAM’;
for i in 1..10
loop
dbms_output.put_line(v_ename);
end loop;
end;
/
OUTPUT:-

QUESTION 2:-
WRITE A PL/SQL BLOCK OF CODE TO INVERT A NUMBER 12345 TO 54321
QUERY:-
Declare
    n number;
    i number;
    rev number:=0;
    r number;
begin
    n:=&n;
    while n>0
    loop
        r:=mod(n,10);
        rev:=(rev*10)+r;
        n:=trunc(n/10);
    end loop;

    dbms_output.put_line('reverse is '||rev);
end;
/
OUTPUT:-
QUESTION 3:-
WRITE A PL/SQL CODE BLOCK TO CALCULATE THE AREA OF CIRCLE FOR A VALUE OF
RADIUS VARYING FROM 3 TO 7. STORE THE RADIUS & THE CORRESPONDING VALUES
OF CALCULATED AREA IN AN EMPTY TABLE NAMED AREAS, CONSISTING OF TWO
COLUMNS, RADIUS & AREA.
Hint: create a table Area(radius,area), insert values into Area table through PL/SQL blocks.
QUERY:-
create table areas ( r number(2), area number (14,2));

declare
r number(5);
area number(14,2);
pi constant number (4,2):=3.14;
begin
r:=3;
while r<=7
loop
area:=pi*power(r,2);
insert into areas values(r,area );
r:=r+1;
end loop;
end;

select * from areas;


OUTPUT:-
QUESTION 4:-
CREATE A SIMPLE LOOP SUCH THAT A MESSAGE IS DISPLAYED WHEN A LOOP EXCEEDS A
PARTICULAR VALUE(WHILE LOOP).
QUERY:-
Declare
a number :=1;
BEGIN
dbms_output.put_line('Program started');
LOOP
dbms_output.put_line(a);
a:=a+1;
Exit when a>5;
end loop;
dbms_output.put_line('Exceeded limit');
end;
/
OUTPUT:-
QUESTION 5:-
WRITE A PL/SQL BLOCK CODE THAT WILL ACCEPT AN ACCOUNT NUMBER FROM THE USER,
CHECK IF THE USER'S BALANCE IS LESS THAN THE MINIMUM BALANCE,
ONLY THEN DEDUCT RS. 100/- FROM THE AVAILABLE BALANCE. THE PROCESS IS FIRED ON
THE ACCOUNTS TABLE(Hint: create a table ACCOUNTS with necessary attributes,
LIKE ACC NO,NAME,LAST DATE OF TRANSACTION,CREDIT,DEBIT,CURRENT BAL, ETC,.).
QUERY:-
create table ACCOUNTS(
ACC_NUM number(10),
NAME varchar2(10),
last_date_transc date,
credit integer,
debit integer,
current_bal integer);

DECLARE
xacct_num number;
xacct_min_bal :=1000;
xbalance integer;
BEGIN
xacct_num :=&xacct_num;
select current_bal into xbalance from ACCOUNTS where ACC_NUM :=&xacct_num;
IF(xbalance< xacct_min_bal) THEN
update ACCOUNTS set current_bal=current_bal-100 where ACC_NUM=xacct_no;
xbalance:=xbalance-100;
dbms_output.put_line('Rs 100 is deducted and current balance is'||xbalance);

ELSE
dbms_output.put_line('Current balance is'||xbalance);
END IF;
END;
OUTPUT:-
QUESTION 6:-
Write a function to find factorial of a number. Also call and check the factorial of a number.
QUERY:-
Declare
factorial number;

create or replace FUNCTION fact(a number)


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

--CALLING FUNCTION FACT()


set serveroutput on;
Declare
a number;
factorial number;
BEGIN
a:= 6;
factorial := fact(a);
dbms_output.put_line(' Factorial '|| a || ' is ' || factorial);
END;
/
OUTPUT:-
QUESTION 7:-
Write a function to find MAXIMUM NUMBER BETWEEN TWO
QUERY:-
SQL> DECLARE
2 a number;
3 b number;
4 c number;
5 FUNCTION findMax(x IN number, y IN number)
6 RETURN number
7 IS
8 z number;
9 BEGIN
10 IF x > y THEN
11 z:= x;
12 ELSE
13 Z:= y;
14 END IF;
15
16 RETURN z;
17 END;
18 BEGIN
19 a:= 23;
20 b:= 45;
21
22 c := findMax(a, b);
23 dbms_output.put_line(' Maximum of (23,45): ' || c);
24 END;
25 /
OUTPUT:-
Maximum of (23,45): 45

PL/SQL procedure successfully completed.


QUESTION 8:-
Write a Procedure using IN and OUT Parameter - to find MAXIMUM NUMBER BETWEEN TWO
QUERY:-
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:-
Minimum of (23, 45) : 23

QUESTION 9:-
Write a Procedure using IN OUT Parameter – to find cube a number. Also call the procedure to give output
for a number.
QUERY:-
set SERVEROUTPUT ON;
create or replace procedure INOUTPARA(p IN OUT number)
AS
BEGIN
p:=p*3;
END INOUTPARA;
--calling
declare
x number;
begin
x:= 15;
INOUTPARA(x);
dbms_output.put_line('The result is '||x);
end;
OUTPUT:-

You might also like