删除由表

时间:2016-11-09 02:52:42

标签: sql sql-server tsql sql-delete table-variable

我有一个名为@workorders的表变量,该表由查询填充:

DECLARE @workorders table
(id_workorder VARCHAR(100));
INSERT INTO @workorders SELECT DISTINCT(id_workorder) FROM CampaingControl WHERE id_campaing = @id;
--get each record of the table @workorders and delete of another table

然后,我想要的是遍历表@workorders的每个记录并删除具有当前记录值的记录。

我要删除记录的表名为WorkOrders,此表有一个名为id_workorder的列,例如:

假设表变量@workorders有3条记录:

  • OT-001-16
  • OT-002-16
  • OT-005-16

我需要实现一种while循环,以获取表@workorders上的每条记录以及表WorkOrders上的每条记录删除id_workorder = @workorders (id_workorder VARCHAR(100))(Current record)

1 个答案:

答案 0 :(得分:3)

您不能简单地INNER JOIN @workorders声明中的DELETE表吗?

DELETE w 
FROM workorders w INNER JOIN @workorders tw ON w.id_workorder = tw.id_workorder