Ticker

6/recent/ticker-posts

Control Statement in Oracle PLSQL - Javainhand

Hello Readers, This is another PLSQL Tutorial blog. In this blog we will discuss Control Statement in Oracle PLSQL.

control statement in oracle plsql

Control Statements in Oracle PLSQL: Making Decisions in Your Code

Control Statements are elements in a program that controls the flow of program execution.

Let's understand with example, Imagine you're cooking. You follow a recipe, but sometimes you need to make choices. "If" the sauce is too thick, you add water. "For" each ingredient, you measure a specific amount. These choices are like "control statements" in PL/SQL – they help your code make decisions and execute different parts based on conditions.

1. IF Statement

The "If" King: This is the most basic control statement. It checks if a condition is true.

Example:-

IF age > 18 THEN
   DBMS_OUTPUT.PUT_LINE('You are an adult.'); 
END IF;

• Explanation:-

o If the variable "age" is greater than 18, the message "You are an adult." will be displayed.

o If "age" is not greater than 18, nothing happens.

2. CASE Statement

The "Multiple Choice" Master: When you have more than two choices, the CASE statement is your friend.

CASE 
    WHEN grade = 'A' THEN
        bonus := 1000;
    WHEN grade = 'B' THEN
        bonus := 500;
    ELSE
        bonus := 0;
END CASE;

Explanation:

o If "grade" is 'A', the "bonus" variable gets 1000.

o If "grade" is 'B', the "bonus" gets 500.

o If "grade" is anything else, the "bonus" gets 0.

3. While Loop

The "Conditional Repeater": This loop checks a condition before each iteration.

Example:-

WHILE age < 18 LOOP
    -- Some code to be executed 
    age := age + 1; 
END LOOP;

Explanation:

o The code within the loop will execute as long as "age" is less than 18.

o In each iteration, the value of "age" is increased by 1.

4. FOR Loop

The "Counter": This loop is perfect for repeating something a specific number of times.

Example:-

FOR i IN 1..10 LOOP
    -- Some code to be executed 
END LOOP;

Explanation:

o The code within the loop will execute 10 times, with the variable "i" taking values from 1 to 10 in each iteration.

That's all for today, I think(Control Statement in Oracle PLSQL) post will be helpful for you. If you like the post then share your view in the comment box.

If you want to watch this topic then click here

Post a Comment

0 Comments