Is it possible to write an SQL UPDATE
that only changes columns if data is actually provided?
Let's say I'm updating a post. A post
has an id
, author
, title
and text
. When updating a post, either or both of title
and text
can change.
Is there an SQL query which can dynamically change the UPDATE
based on the provided data? It would maybe look something like this in Nodejs pg:
const result = await pgClient.query("UPDATE post SET title = $1, text = $2 WHERE userId = $3",
["new title", undefined, "12345"]);
with an additional clause saying something like "if undefined, do not update."