UPDATE
computes new column values per qualifying row
What changes
- Input
- target table, optional FROM/JOIN sources, WHERE
- Output or change
- updated rows of one target table (and variables in SET @v = ...)
Syntax at a glance
[ WITH <common_table_expression> [...n] ] UPDATE [ TOP ( expression ) [ PERCENT ] ] { { table_alias | <object> | rowset_function_limited [ WITH ( <Table_Hint_Limited> [ ...n ] ) ] } | @table_variable } SET { column_name = { expression | DEFAULT | NULL } | { udt_column_name.{ { property_name = expression | field_name = expression } | method_name ( argument [ ,...n ] ) } } | column_name { .WRITE ( expression , ...
Work example
A lab table needs a small, key-targeted value change.
Select the target key first, update one temporary row, and compare the changed value with the intended result.
Check your work Only the selected lab key changes and the final value matches the rule.
Practice SQL
Open in SQL FlowCREATE TABLE #PracticeStatus (ItemId int PRIMARY KEY, Status varchar(12) NOT NULL);
INSERT INTO #PracticeStatus VALUES (1, 'Queued'), (2, 'Queued');
UPDATE #PracticeStatus SET Status = 'Ready' WHERE ItemId = 1;
SELECT ItemId, Status FROM #PracticeStatus ORDER BY ItemId;
DROP TABLE #PracticeStatus;
Open Microsoft Learn for full syntax, permissions, and details for your version.