IF-THEN is the most basic kind of conditional statements in PL/SQL that enables you to specify only a single group of action to be taken. You can also say that this specific group of action is taken only when a condition is evaluated to be true.

Correct me, if I am wrong, I think IF conditional statement is used in almost every programming language. If you disagree then tweet me @RebellionRider and tell me which programming languages do not support IF statements.

IF-THEN Structure

Before jumping into the tutorial let’s quickly understand the structure/syntax of the IF-THEN statement.

IF condition THEN
Statement1;
…
Statement N;
END IF;

In the starting we have the keyword IF which marks the beginning of IF-THEN block followed by a valid condition or a valid expression which will get evaluated followed by another keyword THEN. Similarly the reserved phrase END IF marks the ending of IF-THEN block.  In between we have a sequence of executable statements which will get executed only if the condition evaluated to be true otherwise the whole IF-THEN block will be skipped.

Working

When an IF-THEN statement is executed a condition is evaluated to either True or False. If the condition evaluates to true, control is passed to the first executable statement of the IF-THEN construct. If the condition evaluates to false, control is passed to the first executable statement after the END-IF statement.

Examples:

SET SERVEROUTPUT ON;
DECLARE
  v_num NUMBER := 9;
BEGIN
IF v_num < 10 THEN
 DBMS_OUTPUT.PUT_LINE(‘Inside The IF’);
 END IF;
 DBMS_OUTPUT.PUT_LINE(‘outside The IF’);
END;
/

This is a very simple PL/SQL anonymous block. In the declaration section I have declared a variable v_num with data type NUMBER and initialized it with integer 9.

Let’s come to our execution section. Here as you can see we have 2 DBMS_OUTPUT statements one is inside the IF-THEN block and another is outside it.

The first DBMS_OUTPUT statement will execute only if the condition of our IF-THEN block is evaluated as true otherwise it will be skipped but the 2nd DBMS_OUTPUT statement which is outside the IF-THEN block will execute every time you execute this PL/SQL block.

This means that if the condition is true then both the string INSDIE THE IF and OUTSIDE THE IF will be printed otherwise only OUTSIDE THE IF will be printed.

DECLARE
v_website VARCHAR2(30) := ‘RebellionRider.com’;
v_author  VARCHAR2(30) := ‘Manish’;
BEGIN
IF v_website =’RebellionRider.com’ AND  v_author= ‘Manish’ THEN
DBMS_OUTPUT.PUT_LINE(‘Everything is Awesome :)’);
END IF;
DBMS_OUTPUT.PUT_LINE(‘Give this Video a Thumbs Up’);
END;
/

This one is slightly different than the previous example. Here we used logical AND operator in the condition. If this condition is evaluated to be true then both strings from both DBMS_OUTPUT statements will be printed otherwise only the string from 2nd DBMS_OUTPUT statement will be displayed back to you.

This example is for showing how you can check multiple conditions in a single go using logical operator. You can even use logical OR instead of logical AND operator.

That’s all about IF-THEN conditional statement in PL/SQL. I hope you find this article useful. Kindly please share it on your social networking and help me reach out to more people. Thanks & have a great day!