简体   繁体   English

删除表中记录数给定的记录数

[英]Delete a number of records given by the number of records in a table

I have a table variable called @workorders , this table are filled by a query: 我有一个名为@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

Then, what I want is to go through each record of the table @workorders and delete a record with the value of the current record. 然后,我要遍历表@workorders每个记录并删除具有当前记录值的记录。

The table where I want to delete the records is called WorkOrders , this table have a column called id_workorder , for example: 我要删除记录的表称为WorkOrders ,该表具有名为id_workorder的列,例如:

Assuming the table variable @workorders have 3 records: 假设表变量@workorders具有3条记录:

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

I need to implement a kind of while loop, to get each record on the table @workorders and for each record delete on the table WorkOrders where id_workorder = @workorders (id_workorder VARCHAR(100))(Current record) . 我需要实现一种while循环,以获取表@workorders上的每条记录,并删除表WorkOrders上的每条记录,其中id_workorder = @workorders (id_workorder VARCHAR(100))(Current record)

Can you not simply INNER JOIN the @workorders table in your DELETE statement? 您是否可以简单地在DELETE语句中INNER JOIN @workorders表?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM