Refresh table using if PL / SQL statement

I'm trying to do something like this, but it's hard for me to put it in oracle encoding. A.

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 that does not work. I also tried

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

no luck.

+3
source share
2 answers

I don’t think you need a procedural unit if your actual logic is similar to the previous one.

Assuming this is your task:

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

You can just run ...

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

This will only update records where full_date is null with your new date.

+8
source

What is the purpose of this part?

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

, , complete_date null, task_id = 1?

, , task_id = 1?

0

Source: https://habr.com/ru/post/1781888/


All Articles