--> Sayadasite: DBMS PRACTICLE

Multiple Ads

Search

Menu Bar

DBMS PRACTICLE

1.Write a pl/sql program to print the n given number

 Declare

A number(2);

Begin

For a in 0..10

Loop

Dbms_output.put_line(‘value of a: ‘||a);

End loop;

End;

Output:

value of a:0

value of a:1

value of a:2

value of a:3

value of a:4

value of a:5

value of a:6

value of a:7

value of a:8

value of a:9

value of a:10

 

2.Write a pl/sql program to print the  n numbers

 Declare

A int;

B int;

Begin

A:=0;

B:=10;

While a<b loop

Dbms_output.put_line(‘value of a=’||a);

A:=a+1;

End loop;

End;

Output:

value of a=0

value of a=1

value of a=2

value of a=3

value of a=4

value of a=5

value of a=6

value of a=7

value of a=8

value of a=9

 

3. Write a pl/sql program to find the sum of two numbers

Declare

A int;

B int;

Begin

A:=0;

B:=10;

C:=1;

C:=a+b;

Dbms_output.put_line(‘sum of a and b =’||c);

End;

 

Output:

sum of a and b =10

 

4. Write a pl/sql program to find the greatest of two number

Declare

A int;

B int;

Begin

A:=0;

B:=10;

If (a>b)

then

Dbms_output.put_line(‘greater number is: a =’||a);

Else

Dbms_output.put_line(‘greater number is: b =’||b);

End if;

End;

 

Output:

greater number is: b =10

 

5. Write a pl/sql program to find the factorial of a given number

declare

n number;

fac number:=1;

i number;

 

begin

n:=4;

 

for i in 1..n

loop

fac:=fac*i;

end loop;

 dbms_output.put_line('factorial=’||fac);

end;

6(a).write a pl/sql program by using predefined exception

Select * from emp1;

ID

NAME

SAL

1

a

10000

2

a

10000

4

n

20000

 

declare

v_name emp1.id%type;

begin

select id into v_name from emp1

where id=3;

dbms_output.put_line(v_name);

exception

when no_data_found then

dbms_output.put_line('no data found');

when others then

dbms_output.put_line('exception occured');

end;

 

output:

no data found

6(b).write a pl/sql program by using predefined exception

 declare

v_name emp1.id%type;

begin

select id into v_name from emp1

where id=1;

dbms_output.put_line(v_name);

exception

when no_data_found then

dbms_output.put_line('no data found');

when others then

dbms_output.put_line('exception occured');

end;

 output:

1

 8(a).write a pl/sql program by using user defined exception

 declare

     v_sum     number := 10;

     v_divide number := 2;

     v_result number;

begin

     v_result := v_sum / v_divide;

     dbms_output.put_line('v_result: '||v_result);

exception

     when zero_divide then

     dbms_output.put_line('ZERO_DIVIDE: '||sqlerrm);

end;

 Output:

v_result: 5

 

8(b).write a pl/sql program by using user defined exception

 declare

     v_sum     number := 10;

     v_divide number := 0;

     v_result number;

begin

     v_result := v_sum / v_divide;

     dbms_output.put_line('v_result: '||v_result);

exception

     when zero_divide then

     dbms_output.put_line('ZERO_DIVIDE: '||sqlerrm);

end;

 output:

ZERO_DIVIDE: ORA-01476: divisor is equal to zero

 

 


No comments: