I am trying to write a query that will update my table #master12.cg
when it is null. So here are my steps needed:
1) Check if
#master12.uid
exists in#check1.dasboot
and if it does, then update#master12.cg
with that value
2) Check if#master12.uid
exists in#check.uid
and if it does update#master12.cg
with the value in#check2.redbluegreen
3) If#master12.uid
does not exist in either table then just leave#master12.uid
as null
I have tried this query
Update #master12
Set [cg] = case
when exists (Select [dasboot] FROM [#check1] WHERE [#master]. [uid] = [#check1].[uid])
when not exists (Select [redbluegreen] FROM [#check2] WHERE [#master12].[uid] = [#check2].[uid]
else [#master12].[cg]
end
WHERE [#master12].[cg] IS NULL
However this query presents an error of:
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'when'.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'else'.
How should I write this update statement so that the checks listed above are performed and my table is updated accordingly?