MS SQL Server Concepts and Programming Question: Download MS SQL Server PDF
How To Delete Multiple Rows with One DELETE Statement in MS SQL Server?
Answer:
You can delete multiple rows from a table in the same way as deleting a single row, except that the WHERE clause will match multiple rows. The tutorial exercise below deletes 3 rows from the ggl_links table:
-- view rows to be deleted
SELECT id, url, notes, counts FROM ggl_links
WHERE id > 300
GO
id url notes counts
801 www.globalguideline.com Wrong 1202
802 www.globalguideline.com/html Wrong 1204
803 www.globalguideline.com/sql Wrong 1206
-- delete multiple rows
DELETE FROM ggl_links WHERE id > 300
GO
(3 row(s) affected)
-- try to view the deleted row
SELECT id, url, notes, counts FROM ggl_links
WHERE id > 300
GO
no rows
-- view rows to be deleted
SELECT id, url, notes, counts FROM ggl_links
WHERE id > 300
GO
id url notes counts
801 www.globalguideline.com Wrong 1202
802 www.globalguideline.com/html Wrong 1204
803 www.globalguideline.com/sql Wrong 1206
-- delete multiple rows
DELETE FROM ggl_links WHERE id > 300
GO
(3 row(s) affected)
-- try to view the deleted row
SELECT id, url, notes, counts FROM ggl_links
WHERE id > 300
GO
no rows
Previous Question | Next Question |
How To Delete an Existing Row with DELETE Statements in MS SQL Server? | How To Delete All Rows with TRUNCATE TABLE Statement in MS SQL Server? |