IF Else Statement

Oracle

When it comes to oracle if clause needs to be inside begin and end blocks

 

Syntax:

            IF condition THEN

   [Statements]

ELSIF condition THEN

   [Statements]

ELSE

   [Statements]

END IF;

 

Example:

DECLARE VAR_STRING VARCHAR (100):='Sam1';

-- If we change the above assignment to Sam2 then ELSIF will run

-- If we change the above assignment to something other than Sam1, Sam2 then ELSE will run

BEGIN

IF VAR_STRING='Sam1'

THEN

   DBMS_OUTPUT.PUT_LINE ('If Clause Output');

ELSIF VAR_STRING='Sam2'

  THEN 

  DBMS_OUTPUT.PUT_LINE ('ELSIF Clause Output');

ELSE

 DBMS_OUTPUT.PUT_LINE ('ELSE Clause Output');

END IF;

END;

 

SQL Server

When it comes to SQL Server if clause need not be inside the begin and end blocks but if we need to write multiple statement inside the if clause then we need begin and end blocks

 

Syntax:

            IF condition

   BEGIN

     [Statements]

  END

ELSE IF condition

  BEGIN

     [Statements]

  END

ELSE

  BEGIN

     [Statements]

  END

 

Example:

            DECLARE @VAR_STRING VARCHAR (100)='Sam1';

-- If we change the above assignment to Sam2 then ELSIF will run

-- If we change the above assignment to something other than Sam1, Sam2 then ELSE will run

IF @VAR_STRING='Sam1'

 BEGIN

   PRINT 'If Clause Output';

 END

ELSE IF @VAR_STRING='Sam2'

 BEGIN 

  PRINT 'ELSIF Clause Output';

 END

ELSE

 BEGIN

 PRINT 'ELSE Clause Output';

 END

 

Thank you for reading this article, check out my other SQL Server vs. Oracle posts.