SQL IF is a control-of-flow statement available in T-SQL and some other dialects that lets you branch logic based on a Boolean expression. When the expression is TRUE, the statement or BEGIN...END block that follows runs; otherwise, the optional ELSE branch runs. IF is evaluated at run-time, so the chosen branch is the only one executed, saving resources compared with running both branches. IF can appear in ad-hoc batches, stored procedures, functions, triggers, and scripts. Nesting IF statements is allowed up to 32 levels in SQL Server. Unlike the CASE expression, IF controls which statements execute rather than returning a value inside a single expression.
boolean_expression
(Boolean) - Any expression that evaluates to TRUE, FALSE, or UNKNOWN.statement_block
(SQL) - One or more valid T-SQL statements, wrapped in BEGIN...END when multiple.CASE expression, IIF function, BEGIN END, ELSE, WHILE, GOTO, TRY CATCH
Sybase SQL Server 4.x (early 1990s)
IF controls which statements run; CASE returns a value within a single statement.
Yes. SQL Server allows up to 32 nested IF levels, each with its own BEGIN...END block.
Only inside PL/pgSQL functions or DO blocks. PostgreSQL queries themselves use CASE for conditional values.
Wrap the UPDATE in an IF block that checks your desired condition, optionally adding an ELSE branch for alternative actions.