[英]SQL Server - Delete (x) number of rows depending on a WHERE caluse
I have a scenario where throughout the day, records are set to delete (BOOLEAN VALUE), but are not actually deleted. 我有一种情况,一整天,记录都设置为删除(布尔值),但实际上并未删除。 A list of these values is displayed within a view (let's call it ViewToDelete
), for example: 这些值的列表显示在视图中(我们称其为ViewToDelete
),例如:
ID DELETE
--------------
1 1
4 1
5 1
At the end of the day I want to be able to delete these values present in this view all in one go, can this be achieved and if so how? 归根结底,我希望能够一次性删除此视图中存在的这些值,这可以实现吗?
DELETE FROM mytablename
WHERE ID = ViewToDelete.ID
--THIS WOULD WORK IF THERE WAS 1 VALUE, BUT FOR MULTIPLE VALUES
--IT RETURNS AN ERROR AS I WOULD EXPECT
You can do this by JOIN
ing to the view: 您可以通过JOIN
视图来做到这一点:
Delete M
From MyTableName M
Join ViewToDelete V On V.Id = M.Id
And V.Delete = 1
You can simply write 你可以简单地写
Delete from Viewtodelete where delete=1
If the view fetches all its data from only 1 table. 如果视图仅从1个表中获取所有数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.