PL/SQL Program to Swap two Numbers

Here you will get pl/sql program to swap two numbers with and without using temporary variable.

Method 1: Using Temporary Variable

declare
    a number;
    b number;
    temp number;

begin
    a:=5;
    b:=10;
    
    dbms_output.put_line('before swapping:');
    dbms_output.put_line('a='||a||' b='||b);
    
    temp:=a;
    a:=b;
    b:=temp;
    
    dbms_output.put_line('after swapping:');
    dbms_output.put_line('a='||a||' b='||b);
    
end;
/

Output

before swapping:
a=5 b=10
after swapping:
a=10 b=5

Method 2: Without Using Temporary Variable

declare
    a number;
    b number;

begin
    a:=5;
    b:=10;
    
    dbms_output.put_line('before swapping:');
    dbms_output.put_line('a='||a||' b='||b);
    
    a:=a+b;
    b:=a-b;
    a:=a-b;
    
    dbms_output.put_line('after swapping:');
    dbms_output.put_line('a='||a||' b='||b);
    
end;
/

Leave a Comment

Your email address will not be published. Required fields are marked *