Skip to main content

PL/SQL Conditional Statements

PL/SQL Conditional Statements provide control over the flow of execution within Oracle database programs based on specified conditions. They include IF-THEN-ELSE statements for binary decisions, CASE statements for multiple conditions. These constructs enhance flexibility and logic in data processing, enabling dynamic and responsive behavior in Oracle PL/SQL applications.






IF-THEN-ELSE Statement

The IF-THEN-ELSE statement allows you to execute different blocks of code based on whether a condition evaluates to true or false.

IF condition THEN

   -- Code block executed if condition is true

ELSE

   -- Code block executed if condition is false

END IF;

 

Example:

DECLARE

   x NUMBER := 10;

BEGIN

   IF x > 0 THEN

      DBMS_OUTPUT.PUT_LINE('x is positive');

   ELSE

      DBMS_OUTPUT.PUT_LINE('x is non-positive');

   END IF;

END;

This PL/SQL code snippet demonstrates a basic structure used within Oracle database environments:

1.     DECLARE: This keyword begins the declaration section where variables and constants are defined. In this case, x is declared as a variable of type NUMBER and initialized with the value 10.

2.     BEGIN: Marks the beginning of the executable section of the PL/SQL block where actual logic and statements are written.

3.     IF-THEN-ELSE: This conditional statement checks whether the variable x is greater than 0 (x > 0). If this condition evaluates to true, it executes the statement inside the THEN block, which in this case outputs 'x is positive' using the DBMS_OUTPUT.PUT_LINE procedure. If the condition is false, it executes the statement inside the ELSE block, which outputs 'x is non-positive'.

4.     END IF: Marks the end of the conditional block, closing the IF-THEN-ELSE structure.

5.     END: Marks the end of the PL/SQL block.

 

ELSEIF Clause

You can use ELSIF clauses to test additional conditions after the initial IF condition.

IF condition THEN

   -- Code block executed if condition is true

ELSE

   -- Code block executed if condition is false

END IF;

Example:

DECLARE

   x NUMBER := 10;

BEGIN

   IF x > 0 THEN

      DBMS_OUTPUT.PUT_LINE('x is positive');

   ELSIF x < 0 THEN

      DBMS_OUTPUT.PUT_LINE('x is negative');

   ELSE

      DBMS_OUTPUT.PUT_LINE('x is zero');

   END IF;

END;

This PL/SQL code snippet demonstrates the use of the IF-THEN-ELSIF-ELSE statement structure, which allows for handling multiple conditions sequentially:

1.     DECLARE: This keyword marks the beginning of the declaration section where variables and constants can be initialized. In this example, x is declared as a variable of type NUMBER and assigned the value 10.

2.     BEGIN: This keyword signifies the start of the executable section of the PL/SQL block, where actual logic and statements are written.

3.     IF-THEN-ELSIF-ELSE: This conditional structure evaluates multiple conditions sequentially:

o   IF x > 0 THEN: Checks if the value of x is greater than 0. If true, it executes the statement inside the THEN block, which outputs 'x is positive' using the DBMS_OUTPUT.PUT_LINE procedure.

o   ELSIF x < 0 THEN: If the first condition (x > 0) is false, this condition checks if x is less than 0. If true, it executes the statement inside the ELSIF block, which outputs 'x is negative'.

o   ELSE: If none of the above conditions (x > 0 and x < 0) are true, the code inside the ELSE block is executed. In this case, it outputs 'x is zero'.

4.     END IF;: Marks the end of the conditional block, closing the IF-THEN-ELSIF-ELSE structure.

5.     END;: Marks the end of the PL/SQL block.

 

CASE Statement:

The CASE statement allows you to evaluate multiple conditions and execute different blocks of code based on the value of an expression.

CASE expression

   WHEN value1 THEN

      -- Code block executed if expression equals value1

   WHEN value2 THEN

      -- Code block executed if expression equals value2

   ...

   ELSE

      -- Code block executed if expression doesn't match any previous values

END CASE;

Example:

DECLARE

   x NUMBER := 2;

BEGIN

   CASE x

      WHEN 1 THEN

         DBMS_OUTPUT.PUT_LINE('x is 1');

      WHEN 2 THEN

         DBMS_OUTPUT.PUT_LINE('x is 2');

      ELSE

         DBMS_OUTPUT.PUT_LINE('x is neither 1 nor 2');

   END CASE;

END;

This PL/SQL code snippet demonstrates the use of the CASE statement, which allows for evaluating a specific expression (x in this case) against a list of possible values and executing the corresponding block of code based on the matching value:

1.     DECLARE: This keyword starts the declaration section where variables and constants can be initialized. In this example, x is declared as a variable of type NUMBER and assigned the value 2.

2.     BEGIN: This keyword signifies the start of the executable section of the PL/SQL block, where actual logic and statements are written.

3.     CASE x: This line introduces the CASE statement, which evaluates the value of x.

o   WHEN 1 THEN: If the value of x is 1, the statement inside the WHEN 1 THEN block is executed. In this case, it prints 'x is 1' using the DBMS_OUTPUT.PUT_LINE procedure.

o   WHEN 2 THEN: If the value of x is 2, the statement inside the WHEN 2 THEN block is executed. Here, it prints 'x is 2'.

o   ELSE: If the value of x does not match any of the specified values (1 or 2), the ELSE block is executed. In this case, it prints 'x is neither 1 nor 2'.

4.     END CASE;: Marks the end of the CASE statement, closing the structure.

5.     END;: Marks the end of the PL/SQL block.

 

 

 

 

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog

TechUplift: Elevating Your Expertise in Every Click

  Unlock the potential of data with SQL Fundamental: Master querying, managing, and manipulating databases effortlessly. Empower your database mastery with PL/SQL: Unleash the full potential of Oracle databases through advanced programming and optimization. Unlock the Potential of Programming for Innovation and Efficiency.  Transform raw data into actionable insights effortlessly. Empower Your Data Strategy with Power Dataware: Unleash the Potential of Data for Strategic Insights and Decision Making.

Python Topics

Learning Python can be an exciting and rewarding journey, especially given its versatility and widespread use in various fields like web development, data science, automation, and more. Here's a structured guide to help you learn Python effectively, covering essential topics from beginner to advanced levels. Beginner Level Introduction to Python Installation and setup Python syntax and interactive shell Writing and running your first Python script Basic Concepts Variables and data types (integers, floats, strings, booleans) Basic arithmetic operations String operation Comments and documentation Control Structures Conditional statements ( if ,  elif ,  else ) Loops ( for ,  while ) Data Structures Lists Tuples Dictionaries Sets Functions Defining and calling functions Function arguments and return values Lambda functions Built-in functions Modules and Packages Importing modules Standard library overview (e.g.,  math ,  datetime ,  random ) Installing and using external packages

DAX Functions

These are just some of the many DAX functions available in Power BI. Each  function serves a specific purpose and can be used to perform a wide range of calculations and transformations on your data. Aggregation Functions: SUM : Calculates the sum of values. AVERAGE : Calculates the arithmetic mean of values. MIN : Returns the smallest value in a column. MAX : Returns the largest value in a column. COUNT : Counts the number of rows in a table or column. COUNTA : Counts the number of non-blank values in a column. DISTINCTCOUNT : Counts the number of unique values in a column. Logical Functions: IF : Returns one value if a condition is true and another value if it's false. AND : Returns TRUE if all the conditions are true, otherwise FALSE. OR : Returns TRUE if any of the conditions are true, otherwise FALSE. NOT : Returns the opposite of a logical value. Text Functions: CONCATENATE : Concatenates strings together. LEFT : Returns the leftmost characters from a text string. RIGHT : Ret