oracle 使用 if 语句 PL/SQL 更新表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4521591/
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-18 22:17:01  来源:igfitidea点击:

Update table with if statement PL/SQL

oracleplsqlif-statement

提问by Matt

I am trying to do something like this but am having trouble putting it into oracle coding.

我正在尝试做这样的事情,但在将它放入 oracle 编码时遇到了麻烦。

BEGIN
IF ((SELECT complete_date FROM task_table WHERE task_id = 1) IS NULL)
THEN
 UPDATE task_table SET complete_date = //somedate WHERE task_id = 1;
ELSE
 UPDATE task_table SET complete_date = NULL;
END IF;
END;

But this does not work. I also tried

但这不起作用。我也试过

IF EXISTS(SELECT complete_date FROM task_table WHERE task_id = 1)

with no luck.

没有运气。

回答by Rajesh Chamarthi

I don't think you'd need the procedural block if your actual logic is like the one above.

如果您的实际逻辑与上面的逻辑类似,我认为您不需要程序块。

Assuming this is your task:

假设这是您的任务:

"if the value of complete_date for id 1 is NULL, update it with XXX. Otherwise set it to null".

“如果 id 1 的 complete_date 的值为 NULL,则用 XXX 更新它。否则将其设置为 null”。

You could just run ...

你可以跑...

Update task_table
  set complete_date = nvl2(complete_date,NULL, <**your date**>)
  where task_id = 1;

This will only update those records where the complete_date is null with your new date.

这只会用您的新日期更新那些 complete_date 为空的记录。

回答by Gigatron

What is the intention of this part?

这部分的意图是什么?

IF ((SELECT complete_date FROM task_table WHERE task_id = 1) IS NULL)

Is it to find if there are rows where complete_date is null and task_id = 1?

是否要查找是否存在 complete_date 为 null 且 task_id = 1 的行?

Or is it to check if there are no rows where task_id = 1?

还是要检查是否没有 task_id = 1 的行?