3

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

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.

2 Answers 2

8

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".

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But also if it is not Null then make it null
0

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?

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

1 Comment

If there is a complete date there then the user must want to mark it incomplete so place null there. If there is no complete date then the user must want to mark it complete so place a date there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.