I’m trying to assign an ID code by searching for the name.
update db.request r
set s.CreateByID = (
select r.ID
from db.user u
where r.NameCreateBy = u.Name
)
where r.ID in (23506);
How would I put a case clause for when I can’t find the name in the user table?
The request table has the correct name of the person who created the request. But the createByID column is returning a code referring to another user. So I need to update createByID based on the user ID, disregarding the value shown in the request table.
2
Answers
you can use
COALESCE
if the subquery returnsNULL
amd set a value for example 0Consider using a
JOIN
inside theUPDATE
statement, if you need to match fields between two tables: