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.

Relationships between tables

In Power BI, relationships between tables are essential for creating accurate and insightful reports. These relationships define how data from different tables interact with each other when performing analyses or creating visualizations. Here's a detailed overview of how relationships between tables work in Power BI: Types of Relationships: One-to-one (1:1):   This is the most common type of relationship in Power BI. It signifies that one record in a table can have multiple related records in another table. For example, each customer can have multiple orders. Many-to-One (N:1):   This relationship type is essentially the reverse of a one-to-many relationship. Many records in one table can correspond to one record in another table. For instance, multiple orders belong to one customer. One-to-Many (1:N):   Power BI doesn't support direct one-to-many relationships.  One record in table can correspond to many records in another table.  Many-to-Many (N:N):  ...

SQL Fundamentals

SQL, or Structured Query Language, is the go-to language for managing relational databases. It allows users to interact with databases to retrieve, manipulate, and control data efficiently. SQL provides a standardized way to define database structures, perform data operations, and ensure data integrity. From querying data to managing access and transactions, SQL is a fundamental tool for anyone working with databases. 1. Basics of SQL Introduction : SQL (Structured Query Language) is used for managing and manipulating relational databases. SQL Syntax : Basic structure of SQL statements (e.g., SELECT, INSERT, UPDATE, DELETE). Data Types : Different types of data that can be stored (e.g., INTEGER, VARCHAR, DATE). 2. SQL Commands DDL (Data Definition Language) : CREATE TABLE : Define new tables. ALTER TABLE : Modify existing tables. DROP TABLE : Delete tables. DML (Data Manipulation Language) : INSERT : Add new records. UPDATE : Modify existing records. DELETE : Remove records. DQL (Da...