oracle 如何为 PL/SQL 块的表达式中的变量赋值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9957885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 04:05:58  来源:igfitidea点击:

how to assign value to variable in expression for a PL/SQL block?

oracleplsql

提问by Mahesh Meniya

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

当我运行此脚本时,它在此语句中返回错误 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;

回答by Justin Cave

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

除了在 PL/SQL 中使用 xor 技巧之外,您还可以简单地使用 SQL 语句

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.

回答by A.B.Cade

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

实际上,您还可以通过使用交换异或算法(但您仍然有 3 个命令)来交换两个没有临时编号的数字:

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

至于如何在 plsql 中按位异或看这里

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

恕我直言,在实际程序中应该避免单行,编写它们很有趣,但维护它们却是地狱......

回答by denied

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);

回答by Korhan Ozturk

You cannotmake 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;