PL/SQL Program to Swap two Numbers

Swapping the values of two variables (making a become b and b become a) is one of the most fundamental logic puzzles in programming. In PL/SQL, this is easily accomplished by using a third, temporary variable.

This simple guide will show you the logic and a complete program to solve this problem.

What You Need to Know

To write this program, you will use a few basic PL/SQL concepts:

  1. Enabling Output: You must run this command once in your SQL tool to see the printed results:SET SERVEROUTPUT ON;
  2. Anonymous Block: We will write our code in a DECLARE...BEGIN...END; block.
  3. Variables: We'll need three variables: n1 (our first number), n2 (our second number), and v_temp (a temporary variable to help with the swap).
  4. The "Swap Logic": To swap two numbers, you can't just do n1 := n2 and n2 := n1. This would fail because the original value of n1 would be lost. The correct way is:
    1. Copy n1 into v_temp.
    2. Copy n2 into n1.
    3. Copy v_temp into n2.
  5. Printing the Result: We use DBMS_OUTPUT.PUT_LINE() to show the values before and after the swap.

PL/SQL Program: Swap Two Numbers

This program will take two numbers, n1 and n2, swap their values, and print the results to the screen.

PL/SQL Program

SET SERVEROUTPUT ON;

DECLARE
  -- Define our two numbers
  n1 NUMBER := 10;
  n2 NUMBER := 20;
  
  -- A temporary variable to hold a value during the swap
  v_temp NUMBER;

BEGIN
  
  -- 1. Print the values *before* the swap
  DBMS_OUTPUT.PUT_LINE('--- Before Swap ---');
  DBMS_OUTPUT.PUT_LINE('n1: ' || n1);
  DBMS_OUTPUT.PUT_LINE('n2: ' || n2);
  
  -- 2. Perform the 3-step swap logic
  v_temp := n1;  -- v_temp is now 10
  n1 := n2;      -- n1 is now 20
  n2 := v_temp;  -- n2 is now 10 (from v_temp)
  
  -- 3. Print the values *after* the swap
  DBMS_OUTPUT.PUT_LINE('--- After Swap ---');
  DBMS_OUTPUT.PUT_LINE('n1: ' || n1);
  DBMS_OUTPUT.PUT_LINE('n2: ' || n2);

END;
/

Result

--- Before Swap ---
n1: 10
n2: 20
--- After Swap ---
n1: 20
n2: 10

Program Explanation

  1. DECLARE section: We create n1 (value 10), n2 (value 20), and v_temp (no value, or NULL).
  2. BEGIN section: The program's logic starts.
  3. DBMS_OUTPUT.PUT_LINE(...): First, we print the starting values of n1 (10) and n2 (20).
  4. v_temp := n1;: This is the first step of the swap. We copy the value of n1 (10) into v_temp.
    • n1 = 10, n2 = 20, v_temp = 10
  5. n1 := n2;: This is the second step. We copy the value of n2 (20) into n1. The original value of n1 is now gone, but that's okay because we saved it in v_temp.
    • n1 = 20, n2 = 20, v_temp = 10
  6. n2 := v_temp;: This is the final step. We copy the value from v_temp (10) into n2.
    • n1 = 20, n2 = 10, v_temp = 10
  7. DBMS_OUTPUT.PUT_LINE(...): Now that the swap is complete, we print the final values of n1 (now 20) and n2 (now 10).
Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 25+ years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments