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

Performance Optimization

Performance optimization in SQL is crucial for ensuring that your database queries run efficiently, especially as the size and complexity of your data grow. Here are several strategies and techniques to optimize SQL performance: Indexing Create Indexes : Primary Key and Unique Indexes : These are automatically indexed. Ensure that your tables have primary keys and unique constraints where applicable. Foreign Keys : Index foreign key columns to speed up join operations. Composite Indexes : Use these when queries filter on multiple columns. The order of columns in the index should match the order in the query conditions. Avoid Over-Indexing:  Too many indexes can slow down write operations (INSERT, UPDATE, DELETE). Only index columns that are frequently used in WHERE clauses, JOIN conditions, and as sorting keys. Query Optimization Use SELECT Statements Efficiently : SELECT Only Necessary Columns : Avoid using SELECT * ; specify only ...

DAX UPPER Function

The DAX UPPER function in Power BI is used to convert all characters in a text string to uppercase. This function is useful for standardizing text data, ensuring consistency in text values, and performing case-insensitive comparisons. Syntax: UPPER(<text>) <text>: The text string that you want to convert to uppercase. Purpose: The UPPER function helps ensure that text data is consistently formatted in uppercase. This can be essential for tasks like data cleaning, preparing text for comparisons, and ensuring uniformity in text-based fields. E xample: Suppose you have a table named "Customers" with a column "Name" that contains names in mixed case. You want to create a new column that shows all names in uppercase. UppercaseName = UPPER(Customers[Name]) Example Scenario: Assume you have the following "Customers" table: You can use the UPPER function as follows: Using the UPPER function, you can convert all names to uppercase: UppercaseName = ...

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.