Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Assignment :-4

Q.1. Write a PL/SQL block of code to print Fibonacci series for a given number up to the given range.

Program:-
declare

a number(2):=&a;

b number(2):=0;

c number(2):=1;

d number(2);

i number(2);

begin

dbms_output.put_line('0');

for i in 1..a loop

d:=b+c;

dbms_output.put_line(d);

c:=b;

b:=d;

end loop;

end;

Output:-

Enter value for a: 4

old 2: a number(2):=&a;

new 2: a number(2):=4;

3
Q. 2. Write a program in PL/SQL block of code to reverse a given string.
Program:
declare

str varchar2(20):='&str';

str1 varchar2(20);

len number;

i number;

begin

len:=length(str);

for i in reverse 1..len

loop

str1:=str1 || substr(str,i,1);

end loop;

dbms_output.put_line(str1);

end;

OutPut:

Enter value for str: vijay

old 2: str varchar2(20):='&str';

new 2: str varchar2(20):='vijay';

yajiv

Q.3. Write a program in PL/SQL block of code to find the square of the given number.
Proragm:
declare

a number:=&a;

i number;

data number;

d number;
begin

for i in 1..a

loop

data:=a*i;

end loop;

dbms_output.put_line(data);

end;

OutPut:

Enter value for a: 8

old 2: a number:=&a;

new 2: a number:=8;

64

Q.4. Write a program in PL/SQL block of code to check whether the given character is vowel or
consonant.
Program:
declare

a varchar2(2):='&a';

begin

if (a='a' or a='e' or a='i' or a='o' or a='u') then

dbms_output.put_line('vowel');

else

dbms_output.put_line('consonent');

end if;

end;

OutPut:

Enter value for a: a


old 2: a varchar2(2):='&a';

new 2: a varchar2(2):='a';

vowel

PL/SQL procedure successfully completed.

Q.5. Write a program in PL/SQL block of code to retrieve record from a table.
Program:
declare
c_code customer.CUSTOMER_CODE % type;

c_name customer.CUSTOMER_NAME % type;

c_city customer.CUSTOMER_CITY % type;

cursor My_cursor is select CUSTOMER_CODE,CUSTOMER_NAME,CUSTOMER_CITY from customer;

begin

open My_cursor;

loop

fetch My_cursor into c_code,c_name,c_city;

exit when My_cursor %notfound ;

dbms_output.put_line( c_code || c_name ||c_city);

end loop;

end;

Output:

C00013 Michel London

C00001 Michel New York

C00020 Albert New York

C00025 Ravindra Bangalore

C00024 Cook Londan

C00015 Stuart Londan


C00002 Bolt New York

C00018 Flaming Brisbane

C00021 Jack Brisbane

Q.6. Write a program in PL/SQL block of code to interchange two given number.
Program:
declare

a number:=&a;
b number:=&b;

c number;

begin

c:=b;

b:=a;

a:=c;

dbms_output.put_line('a value is' ||a);

dbms_output.put_line('b value is' ||b);

end;

Output:

Enter value for a: 5

old 2: a number:=&a;

new 2: a number:=5;

Enter value for b: 6

old 3: b number:=&b;

new 3: b number:=6;

a value is 6

b value is 5
Q.7. Write a program in PL/SQL block of code to create a procedure to square of a given number.5
Program:
declare

a number:=&a;

b number;

procedure fun(x in number,z out number)is

i number;

begin

for i in 1..x

loop

z:=a*i;

end loop;

end;

begin

fun(a,b);

dbms_output.put_line('answer:' || b);

end;

Output:

Enter value for a: 6

old 2: a number:=&a;

new 2: a number:=6;

answer : 36

Q.8. Write a program in PL/SQL block of code to create a function to find factorial of a given number.
Program:
declare
a number:=&a;

b number;

function fun(x in number) return number

is

i number;

fact number:=1;

begin

for i in 1..x

loop

fact:= fact*i;

end loop;

return fact;

end;

begin

b:=fun(a);

dbms_output.put_line('answer:' || b);

end;

Output:

Enter value for a: 5

old 2: a number:=&a;

new 2: a number:=5;

answer:12

PL/SQL procedure successfully completed.

Q.9. Write a program in PL/SQL block of code to create a procedure to make sum of digit for a given
number like given number is 1234=10.
Program:
declare

n number:=&n;

get number;
procedure sum(x in number,y out number) is

z number;

b number;

temp number:=0;

r number;

begin

z:=x;

while z<>0

loop

r :=MOD(z,10);

temp:=temp+r;

z:= Trunc(z/10);

end loop;

y:=temp;

dbms_output.put_line('Ok');

end;

begin

sum(n,get);

dbms_output.put_line('the number is :' ||get);

end;

Output:

Enter value for n: 782

old 2: n number:=&n;

new 2: n number:=782;

Ok

the number is :17

PL/SQL procedure successfully completed.


Q.10. Write a program in PL/SQL block of code to create a statement trigger and apply on product
table and store value in product_check table.
Program:
Create Trigger:-
create or replace trigger tri

after insert on product

for each row

begin
insert into sub1 values(1,'vijay',5000);

dbms_output.put_line('row lelvel triger');

end;

/*When i am insert in product table:-

insert into product values(2,'ajay',5000);

Then also value insert in sub1 table./*

Output:-

SQL> select * from sub1;

ID PRODUCT_NAME PRODUCT_PRICE

---------- ------------------------------ -------------

1 vijay 5000

Q.11. Write a program in PL/SQL block of code to create a row level trigger and apply it on product
table and store value in product_check table.
Program:
create or replace trigger tri

after UPDATE on product


begin

insert into sub1 values(1,'vijay',5000);

dbms_output.put_line('row lelvel triger');

end;

Output:

SQL> update product set product_name='vijay';

4 rows updated.

SQL> select * from sub1;

ID PRODUCT_NAME PRODUCT_PRICE

---------- ------------------------------ -------------

1 vijay 5000

You might also like