Control structures are fundamental to any programming language, and PL/SQL is no exception. They allow you to control the flow of execution in your PL/SQL programs. In this section, we will cover the following control structures:

  1. Conditional Control: IF Statements
  2. Iterative Control: LOOP Statements
  3. Sequential Control: GOTO Statements

  1. Conditional Control: IF Statements

The IF statement is used to execute a block of code based on a condition. PL/SQL supports several forms of the IF statement:

Simple IF Statement

IF condition THEN
    -- statements to execute if condition is true
END IF;

Example

DECLARE
    v_salary NUMBER := 3000;
BEGIN
    IF v_salary > 2500 THEN
        DBMS_OUTPUT.PUT_LINE('Salary is above 2500');
    END IF;
END;
/

IF-THEN-ELSE Statement

IF condition THEN
    -- statements to execute if condition is true
ELSE
    -- statements to execute if condition is false
END IF;

Example

DECLARE
    v_salary NUMBER := 2000;
BEGIN
    IF v_salary > 2500 THEN
        DBMS_OUTPUT.PUT_LINE('Salary is above 2500');
    ELSE
        DBMS_OUTPUT.PUT_LINE('Salary is 2500 or below');
    END IF;
END;
/

IF-THEN-ELSIF-ELSE Statement

IF condition1 THEN
    -- statements to execute if condition1 is true
ELSIF condition2 THEN
    -- statements to execute if condition2 is true
ELSE
    -- statements to execute if none of the above conditions are true
END IF;

Example

DECLARE
    v_salary NUMBER := 2500;
BEGIN
    IF v_salary > 3000 THEN
        DBMS_OUTPUT.PUT_LINE('Salary is above 3000');
    ELSIF v_salary = 3000 THEN
        DBMS_OUTPUT.PUT_LINE('Salary is exactly 3000');
    ELSE
        DBMS_OUTPUT.PUT_LINE('Salary is below 3000');
    END IF;
END;
/

  1. Iterative Control: LOOP Statements

PL/SQL provides several types of loops to execute a block of code multiple times:

Basic LOOP

LOOP
    -- statements to execute
    EXIT WHEN condition;
END LOOP;

Example

DECLARE
    v_counter NUMBER := 1;
BEGIN
    LOOP
        DBMS_OUTPUT.PUT_LINE('Counter: ' || v_counter);
        v_counter := v_counter + 1;
        EXIT WHEN v_counter > 5;
    END LOOP;
END;
/

WHILE LOOP

WHILE condition LOOP
    -- statements to execute
END LOOP;

Example

DECLARE
    v_counter NUMBER := 1;
BEGIN
    WHILE v_counter <= 5 LOOP
        DBMS_OUTPUT.PUT_LINE('Counter: ' || v_counter);
        v_counter := v_counter + 1;
    END LOOP;
END;
/

FOR LOOP

FOR counter IN lower_bound..upper_bound LOOP
    -- statements to execute
END LOOP;

Example

BEGIN
    FOR v_counter IN 1..5 LOOP
        DBMS_OUTPUT.PUT_LINE('Counter: ' || v_counter);
    END LOOP;
END;
/

  1. Sequential Control: GOTO Statements

The GOTO statement transfers control to a labeled statement. It is generally discouraged due to its potential to create complex and hard-to-read code.

Syntax

<<label_name>>
-- statements

GOTO label_name;

Example

DECLARE
    v_counter NUMBER := 1;
BEGIN
    <<start_loop>>
    DBMS_OUTPUT.PUT_LINE('Counter: ' || v_counter);
    v_counter := v_counter + 1;
    IF v_counter <= 5 THEN
        GOTO start_loop;
    END IF;
END;
/

Practical Exercises

Exercise 1: Using IF Statements

Write a PL/SQL block that checks if a number is positive, negative, or zero and prints an appropriate message.

DECLARE
    v_number NUMBER := -5;
BEGIN
    IF v_number > 0 THEN
        DBMS_OUTPUT.PUT_LINE('The number is positive');
    ELSIF v_number < 0 THEN
        DBMS_OUTPUT.PUT_LINE('The number is negative');
    ELSE
        DBMS_OUTPUT.PUT_LINE('The number is zero');
    END IF;
END;
/

Exercise 2: Using LOOP Statements

Write a PL/SQL block that prints the first 10 natural numbers using a FOR loop.

BEGIN
    FOR v_counter IN 1..10 LOOP
        DBMS_OUTPUT.PUT_LINE('Number: ' || v_counter);
    END LOOP;
END;
/

Exercise 3: Using GOTO Statements

Write a PL/SQL block that uses a GOTO statement to print numbers from 1 to 5.

DECLARE
    v_counter NUMBER := 1;
BEGIN
    <<start_loop>>
    DBMS_OUTPUT.PUT_LINE('Counter: ' || v_counter);
    v_counter := v_counter + 1;
    IF v_counter <= 5 THEN
        GOTO start_loop;
    END IF;
END;
/

Common Mistakes and Tips

  • Overusing GOTO: Avoid using GOTO statements as they can make your code difficult to read and maintain.
  • Infinite Loops: Ensure that your loops have a proper exit condition to avoid infinite loops.
  • Condition Order: In IF-THEN-ELSIF-ELSE statements, order your conditions from most specific to most general to ensure correct execution.

Conclusion

In this section, we covered the essential control structures in PL/SQL, including conditional control with IF statements, iterative control with various LOOP statements, and sequential control with GOTO statements. Understanding and using these control structures effectively will allow you to write more dynamic and flexible PL/SQL programs. In the next section, we will delve into cursors, which are crucial for handling query results in PL/SQL.

© Copyright 2024. All rights reserved