EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial IF THEN ELSE in Oracle
Secondary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

IF THEN ELSE in Oracle

By Priya PedamkarPriya Pedamkar

IF THEN ELSE in Oracle

Introduction to IF THEN ELSE in Oracle

In Oracle IF THEN ELSE is a branching statement. This statement enables us to specify two different groups of statements for execution. This statement accepts Boolean expression as a condition, depending on the value of that expression IF THEN ELSE STATEMENT executes or skips a sequence of statements.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,112 ratings)

Syntax

IF Condition
THEN
Statement 1;
ELSE
Statement 2;
END IF;

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Explanation:

  • IF: It is a reserved word and marks the beginning of the IF statement
  • Condition: This is a Boolean expression that returns a Boolean result.
  • Statement 1: This is a statement which executes if the condition evaluates the TRUE result
  • ELSE: It executes if the condition returns the FALSE result. It is an option which enables statement.
  • Statement 2:This is a statement that executes if the condition returns a result.
  • ENDIF: This keyword closes or terminates the IF THEN ELSE block or statement

Flowchart of IF THEN ELSE in Oracle

Flowchart of IF … THEN … ELSE Block

Flow chart of IF THEN ELSE Block

Explanation: Both flowcharts clearly show that how IF THEN ELSE statement works. IF block accepts the condition and evaluates to TRUE / FALSE. If the condition returns TRUE then go to the “THEN” Block and executes the “STATEMENT 1” and then execute END block. If the condition returns FALSE then go to the “ELSE” Block and execute “STATEMENT 2” and then execute END block. If the condition evaluates to ‘NULL’ then ‘NULL’ will be treated as ‘FALSE’ then go to the “ELSE” Block and execute “STATEMENT 2” and then execute END block.

General Points to Ponder About It

Before implementing of Oracle IF THEN ELSE statement, must know some important points about IF THEN ELSE construct:

  • “IF … THEN … ELSE” is a reserved word and marks the beginning of the “IF” statement.
  • This enables us to specify two different groups of statements for execution.
  • One group is evaluated when the condition evaluates to “TRUE”, the next group is evaluated when the condition evaluates to “FALSE”.
  • The “END IF” is a reserved word that indicates the end of the “IF … THEN … ELSE” construct.
  • When “IF … THEN” is executed, a condition is evaluated to either “TRUE” or “FALSE”.
  • The condition need not be specified in brackets.
  • Condition(s) is / are compared by using either the comparison operator(s) or SQL * PLUS operator(s).
  • One “IF” keyword can manage any number of conditions at a point of time using the Logical Connectivity like “AND”, “OR”.
  • Even though the multiple conditions need not be constrained by using brackets, it is better to control their precedence using the proper implementation of brackets to avoid ambiguity.
  • Every “IF” that is implemented needs compulsorily a “TRUE” state evaluation for its successful execution. But an evaluation state is not mandatory when the condition is “FALSE”.
  • After the actual job of “IF” has been completed, the control structure returns the job to the original state of PL / SQL block.

Example to Implement IF THEN ELSE in Oracle

In this section, we’ll see the implementation of Oracle IF … THEN … else how this control structure works in Oracle using some examples. For that, we will use the below sample table (Employee & Dept_category) if required with 14 & 8 records to understand the Oracle IF … THEN … ELSE behavior.

Code:

DECLARE
V_num NUMBER: = &EnterNumber;
BEGIN
IF MOD (V_num, 2) =0 THEN
DBMS_OUTPUT.PUT_LINE (V_num ||' is an even number.');
ELSE
DBMS_OUTPUT.PUT_LINE (V_num ||' is an odd number.');
END IF;
END;

Output:

IF THEN ELSE in Oracle

Explanation:

&EnterNumber: Here “&” enables a user to enter a value within the range and data type criteria.

  • MOD (m, n): It is a predefined function that returns the remainder of being’ divided by ‘n’.
  • DBMS_OUTPUT: DBMS_OUTPUT is an Oracle inbuilt package that allows us to write data to direct output to a screen. This package is used for displaying debugging information.
  • PUT_LINE: It is a procedure that displays the information or results in a line

If MOD function returns 0 then equality condition evaluates “TRUE”  and executes the THEN block statement else executes ELSE block.

As the output showing “ PL/SQL procedure completed” means no error in the above code. But the output is not throwing an expected result. WHY?

It not showing an expected result because there is no setting up control to display output. To set the control, need to execute once “ SET SERVER OUTPUT  ON” in the current user session and it allows the buffer to display the output. See the output for the same example after running the  “ SET SERVER OUTPUT  ON” in the session.

NULL effect

In this output, we got the expected output “10 is an even number”.

The NULL effect in the above PL/SQL Program

NULL effect

In the above output entered value is NULL and in that scenario IF … THEN … ELSE executes the FALSE (ELSE) block that’s why output shows NULL in the place of V_num.

Tips: Whenever a conditional process is executed it is better to cross verify the NULL status of any variable or value before execution. To run any PL/ SQL program use forward slash “/” key. NULL must be handled carefully, NULL can be handled using nested IF or using some other condition.

Conclusion

Oracle IF … THEN … ELSE is a control structure that can be very useful when trying to choose between two mutually exclusive actions. It is used in the PL/SQL block which enables a programmer to handle the real-time requirement in a proper way. This structure operates on Boolean expression and Boolean result.

Recommended Articles

This is a guide to IF THEN ELSE in Oracle. Here we discuss an introduction to IF THEN ELSE in Oracle with syntax, flowchart, and example. You can also go through our other related articles to learn more –

  1. Oracle Operators
  2. Cursor in Oracle
  3. Oracle Queries
  4. Oracle vs MSSQL
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more