Thursday, November 17, 2011

Basic LOOP Program in PL/SQL

It is a PL/SQL program which show how to use basic loop concept in PL/SQL block.

Aim - PL/SQL program to print 1 to 10 using basic loop.


Sample Program: -
declare
  num number(3);
begin
  num:=1;
  loop
     dbms_output.put_line(num);
     num:=num+1;
     exit when num>10;  
  end loop;
end;

Output: -
1
2
3
4
5
6
7
8
9
10

PLSQL FOR Loop Example

It is a basic FOR loop example program which is demonstrate how to use FOR loop in PL/SQL block. FOR loop use in PLSQL is quite differ with C program FOR loop syntax. In PLSQL you should specify the range of integer to execute the sequence of statements.

Syntax
FOR variable IN start..end
LOOP
 PL/SQL block statements
END LOOP;


Sample Example to print number 1 to 10 using FOR loop.

Aim - pl/sql program to print 1 to 10 using FOR loop


Program Code: -
declare
  num number(3);
begin
  for num in 1..10
 loop
  dbms_output.put_line(num);
 end loop;
end;


Output: -
1
2
3
4
5
6
7
8
9
10



Another example to print 1 to 10 in reverse order using FOR loop.
Syntax
FOR variable IN REVERSE start..end
LOOP
PL/SQL block statements
END LOOP;



Aim - program to print 1 to 10 in reverse order using FOR loop.

Program Code: -
declare
  num number(3);
begin
  for num in  REVERSE 1..10
 loop
  dbms_output.put_line(num);
 end loop;
end;


Output: -
10
9
8
7
6
5
4
3
2
1

Wednesday, November 16, 2011

PLSQL Example Find Even Odd Number

It is a sample plsql example code to find accepted number is even or odd and print output with friendly message.

Even or Odd Number
Even number is a number which is completely divided by 2 and reminder is 0 like 2, 4, 6, 8, 10, ..
Odd number is a number which is completely not divided by 2 and reminder is not 0 like 1, 3, 5, 7, 9, ..


Aim - PLSQL Example to find accepted number is even or odd


Program Code : -

declare
  num number(5);
begin
  num:=#
  if num mod 2 =0 then
    dbms_output.put_line('Accepted Number '||num||' is even');
  else
    dbms_output.put_line('Accepted Number '||num||' is odd');
  end if;
end;



Output of PLSQL Block:-
Enter value for num: 10
old 5: num:=&num
new 5: num:=10;
Accepted Number 10 is even


Saturday, November 12, 2011

PL/SQL Example to find average of three numbers

This is a PL/SQL sample example to accept three numbers from the user and find average of accepted numbers.

Aim - PL/SQL Example to find average of three numbers



Sample Program : -


declare
a number(3);
b number(3);
c number(3);
avgr number(5,2);
begin
a:=&a;
b:=&b;
c:=&c;
avgr:=((a+b+c)/3);
dbms_output.put_line('Average of a, b, c is: '||avgr);
end;


Output of PL SQL Block: -

Enter value for a: 22
old 7: a:=&a;
new 7: a:=22;
Enter value for b: 32
old 8: b:=&b;
new 8: b:=32;
Enter value for c: 43
old 9: c:=&c;
new 9: c:=43;
Average of a, b, c is: 32.33