【问题标题】:PL/SQL using IF statement inside Update在更新中使用 IF 语句的 PL/SQL
【发布时间】:2016-01-07 01:21:25
【问题描述】:

想知道是不是这样的:

begin
    for C2 in cursor_sal loop
        if something then
            update emp
            set name = George
            where ID = 1
        elsif something2 then
            update emp
            set name = Steve
            where ID = 4
        end if
    end loop;
end;

可以变成这样或类似的东西:

begin
    for C2 in cursor_sal loop
        update emp
        if something then
            set name = George
        elsif something2 then
            set name = Steve
        end if
        where current of C2
    end loop;
end;

或者这是不可能的,我坚持第一个例子?

【问题讨论】:

  • 你可以使用变量。
  • 如何使用变量来在 Update 语句中使用 IF 语句?会不会和我在第一个例子中的完全一样?

标签: plsql sql-update cursor


【解决方案1】:

我所知道的最好的方法是按照下面的示例使用 case 语句。代码未经测试,但应该足以让您继续。

begin
    for C2 in cursor_sal loop
        update emp
        set name = case
                     when something     then 'George'
                     when somethingelse then 'something2'
                     else 'somthing 3'
                   end
        where current of C2
    end loop;
end;

【讨论】:

    【解决方案2】:

    你可以像这样在更新语句中使用case语句

    update emp
      set name = case when something then 'George'
                      when something2 then 'Steve'
                 end;
    

    如果条件相等,也可以使用解码功能。

    update 是 sql 语句,if 语句是 pl/sql 构造。您可以在 pl/sql 中使用 sql 语句,但不能在 sql 中使用 pl/sql 构造。

    【讨论】:

      猜你喜欢
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多