UPDATE is used to change data in a table.
UPDATE, we can change the data in our table to something else.
And we have the flexibility to update the table such that we change either:
- the entire table
- a subset
- a single value
Furthermore, we can use a
CASE statement to apply our update via branching logic.
Let’s set the entire
Duration column to
UPDATE dbo.Song SET Duration = NULL;
Duration for a single song in
UPDATE dbo.Song SET Duration = 600 WHERE SongID = 1;
Let’s subset the tuples that match to another table
dbo.GoodSongs and update the
UPDATE A SET A.Description = 'Good Song' FROM dbo.Song AS A WHERE EXISTS (SELECT 1 FROM dbo.GoodSongs WHERE A.SongID = SongID)
Let’s update the entire table. But use a case statement to update the
Description field based on
UPDATE dbo.Song SET Description = CASE WHEN Duration >= 300 THEN 'Long Song' ELSE 'Short Song' END