Listing 2 Code That Deletes Multiple Records from a Table by Using a View as the Filter USE pubs GO -- Drop t1, t2, and v1 if they already exist. IF OBJECT_ID('t1') IS NOT NULL DROP TABLE t1 IF OBJECT_ID('t2') IS NOT NULL DROP TABLE t2 IF OBJECT_ID('v1') IS NOT NULL DROP VIEW v1 GO -- Create table t1 and insert five rows. CREATE TABLE t1 (id INT IDENTITY(1,1), col1 INT DEFAULT 27) INSERT t1 DEFAULT VALUES INSERT t1 DEFAULT VALUES INSERT t1 DEFAULT VALUES INSERT t1 DEFAULT VALUES INSERT t1 DEFAULT VALUES GO -- Create table t2 and insert five rows. CREATE TABLE t2 (id int IDENTITY(1,1), col1 int DEFAULT 27) INSERT t2 DEFAULT VALUES INSERT t2 DEFAULT VALUES INSERT t2 DEFAULT VALUES INSERT t2 DEFAULT VALUES INSERT t2 DEFAULT VALUES GO -- Create view v1, which returns a number of rows. CREATE VIEW v1 AS SELECT t2.id AS my_key ,t2.col1 AS my_value FROM t2 WHERE t2.id > 3 -- 3 is arbitrary GO -- Take a peek at t1 before deletion. SELECT * FROM t1 GO -- Take a peek at v1 before deletion. SELECT * FROM v1 GO -- Delete rows in table t1, which has an ID that matches v1.my_key in view v1. DELETE FROM t1 WHERE EXISTS (SELECT * FROM v1 WHERE t1.id = v1.my_key) GO -- Take a peek at t1 after deletion; rows 4 and 5 are deleted. SELECT * FROM t1 GO -- Drop objects that the sample code created. DROP TABLE t1 DROP TABLE t2 DROP VIEW v1 GO GO DROP TABLE dbo.t2 GO DROP VIEW dbo.v1 GO