Lesson 19. Deleting Data

Deleting data is a dangerous operation. Like INSERT, DELETE operates on entire rows. You need to be extremely careful with its usage. It is not hard to blow away an entire table because coding got monotonous and your attention drifted. In order to prevent this catastrophe from happening, I recommend that you verify what you are deleting with a SELECT statement before you write your DELETE.

DELETE Syntax

DELETE --That is it. Just delete.

FROM --The table you want to delete from.

WHERE --The thing that saves you from being fired.

Examples

Example Deletion Of One Record

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

--Begin delete process
DELETE 
FROM @Person
WHERE ID = 1

SELECT *
FROM @Person

Last updated