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.
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.
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!
You can DOWNLOAD SQL script and presentation used in the Video and in this article.