Saturday, April 27, 2013

ORACLE PL SQL IF THEN STATEMENT

PL SQL  IF THEN STATEMENT
              IF statement is used for conditional execution.If Condition is true then code will be executed,otherwise control goes to next executable statement in the PL/SQL block.


SYNTAX:
               IF CONDITION THEN
                  begin
                           --statements here
                   end;
                END IF;


Example 1: Comparing 2 numbers;
SQL>declare
i integer:=20;
j integer:= 20;
begin
   if( i = j)
    then
                   dbms_output.put_line('i=j');
    end if;
end;

SQL>/
i=j

PL/SQL procedure successfully completed.

Example 1: BOOLEAN VALUE CONDITION COMPARISION

SQL>declare
b boolean:=true;
begin
   if( b)
    then
                   dbms_output.put_line('b is true');
    end if;
end;
/
SQL> /
b is true

PL/SQL procedure successfully completed.

 Example 1: COMPARE STRINGS for equality

declare
a varchar2(10):='PARIS';
b varchar2(10):='PARIS';
begin
   if(a= b)
    then
                   dbms_output.put_line('a'|| '&'|| 'b  is same');
    end if;
end;

SQL> /
a&b  is same

PL/SQL procedure successfully completed.
 

 Example 1: COMPARING 2 DATE for equality

SQL>declare
a date:=to_date('1997-12-12','yyyy-MM-dd');
b date:=to_date('1997-12-12','yyyy-MM-dd');
begin
   if(a= b)
    then
                   dbms_output.put_line('a'|| '&'|| 'b  has same dates');
    end if;
end;











SQL>/
 a&b has same dates

PL/SQL procedure successfully completed.

 Example 1: COMPARING 2 DATE for non-equality

declare
a date:=to_date('1997-12-12','yyyy-MM-dd');
b date:=to_date('1998-12-12','yyyy-MM-dd');
begin
   if(a <> b)
    then
                   dbms_output.put_line('a'|| '&'|| 'b  dates are different');
    end if;
end;

SQL>/
a&b dates are different

PL/SQL procedure successfully completed.


Tags: ORACLE PL SQL  IF THEN STATEMENT ,PL/SQL IF Statement, PL/SQL IF statement syntax,using PL/SQL IF statement, COMAPARE 2 strings,Compare dates,Compare number variables, Check Boolean value using if statement.

No comments:

Post a Comment