3

When I run this script it returns error in this statement no1:=(no1+no2)-(no2:=no1);

declare
    no1 number(3):=31;
    no2 number(3):=34;
begin
    dbms_output.put_line('Before swap');
    dbms_output.put_line('No1 : '||no1||'  No2 : '||no2 );
--  no1:=(no1+no2)-(no2:=no1);  generate error
    dbms_output.put_line('After swap');
    dbms_output.put_line('No1 : '||no1||'  No2 : '||no2 );
end;
Mahesh Meniya
  • 2,627
  • 3
  • 18
  • 17
  • You can't assign a value in the middle of another assignation. You have 2 `:=` in that line. Remove one of them... – Ben Mar 31 '12 at 17:02
  • Why oracle does not provide such facility for assignment any idea? – Mahesh Meniya Mar 31 '12 at 17:06
  • 4
    Because there's no need to probably. You can do one assignment on the line above then use that value in the new line. – Ben Mar 31 '12 at 17:08

4 Answers4

13

In addition to using the xor trick in PL/SQL, you can simply use a SQL statement

DECLARE
  a number := 17;
  b number := 42;
BEGIN
  SELECT a, b
    INTO b, a
    FROM dual;
  dbms_output.put_line( 'a = ' || a );
  dbms_output.put_line( 'b = ' || b );
END;

which swaps the two variables without using a temp variable

SQL> ed
Wrote file afiedt.buf

  1  DECLARE
  2    a number := 17;
  3    b number := 42;
  4  BEGIN
  5    SELECT a, b
  6      INTO b, a
  7      FROM dual;
  8    dbms_output.put_line( 'a = ' || a );
  9    dbms_output.put_line( 'b = ' || b );
 10* END;
SQL> /
a = 42
b = 17

PL/SQL procedure successfully completed.
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
3

Actually, you can also swap two numbers with no temp number, by using the Swap XOR Algorithm (but you'll still have 3 commands):

declare
  no1 number(3):=31;
  no2 number(3):=34;
begin
  dbms_output.put_line('Before swap');
  dbms_output.put_line('No1 : '||no1||'  No2 : '||no2 );

  n1 := (n1 + n2) - bitand(n1,n2) * 2;
  n2 := (n2 + n1) - bitand(n2,n1) * 2;
  n1 := (n1 + n2) - bitand(n1,n2) * 2;

  dbms_output.put_line('After swap');
  dbms_output.put_line('No1 : '||no1||'  No2 : '||no2 );
end;

As to how do bitwise xor in plsql see here

IMHO, one should avoid one-liners in real programs, it's fun to write them but hell to maintain them...

A.B.Cade
  • 16,735
  • 1
  • 37
  • 53
2

You cannot make multiple assignment operations in a single statement, so that will keep generating errors. Instead, I suggest you to define a temp variable and use it for your swap operation, like the following:

declare
  no1 number(3):=31;
  no2 number(3):=34;
  temp number;
begin
  dbms_output.put_line('Before swap');
  dbms_output.put_line('No1 : '||no1||'  No2 : '||no2 );
  --  no1:=(no1+no2)-(no2:=no1);  generate error
  temp := no1;
  no1 := no2;
  no2 : temp;
  dbms_output.put_line('After swap');
  dbms_output.put_line('No1 : '||no1||'  No2 : '||no2 );
end;
Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
  • Ok I got it but I want to swap values without temp variable and that is no1:=no1+no2; no2:=no1-no2; no1:=no1-no2; – Mahesh Meniya Mar 31 '12 at 17:27
  • 2
    Unfortunately, that doesn't seem possible since it is against the nature of consept of variable assignment. So I don't think that it is possible in any programming language. You may want to check [PL/SQL Fundamentals](http://docs.oracle.com/cd/B10500_01/appdev.920/a96624/02_funds.htm#3796) for further details. – Korhan Ozturk Mar 31 '12 at 17:31
  • It works in c/c++ and hope work in modern programming languages. – Mahesh Meniya Mar 31 '12 at 17:40
  • @KorhanÖztürk, it's also possible in python – Ben Mar 31 '12 at 17:45
  • @KorhanOzturk It's possible in [Python](http://stackoverflow.com/q/14836228/1394393). (It would simply be `no1, no2 = no1 + no2, no1 - no2`.) I'm sure it's possible in other languages, too. – jpmc26 Feb 03 '17 at 19:20
2

You can declare an additional procedure with in-out parameters like this :

PROCEDURE swap(a IN OUT NUMBER, b IN OUT NUMBER) is
    buff NUMBER;
BEGIN
    buff := a;
    a := b;
    b := buff;
END swap;

and use it like this :

swap(a, b);
denied
  • 598
  • 3
  • 9