Lesson 18. Updating Data

You update data with the SQL UPDATE statement. When you update data and you only want to update a single row, make sure you have a filter on your query of exactly what you are updating, otherwise you are going to wind up updating the entire table.

UPDATE Syntax

UPDATE --the table you want to update

SET --the new value in the column you want to change

WHERE --The conditions you specify

Examples

Simple Update Example

USE Demo

DECLARE @Person AS TABLE (ID INT, FName NVARCHAR(100), LNAME NVARCHAR(100))

INSERT INTO @Person(ID, FName, LNAME)
SELECT 1,'Bob', 'Wakefield'
UNION
SELECT 2,'Vanna','White'
UNION
SELECT 3,'Pat','Sajak'


SELECT *
FROM @Person
WHERE ID = 1

--Begin update process
UPDATE @Person
SET LNAME = 'Johnson'
WHERE ID = 1

SELECT *
FROM @Person
WHERE ID = 1

Last updated