TimeToCode TimeToCode - 1 year ago 74
SQL Question

Delete a number of records given by the number of records in a table

I have a table variable called

, this table are filled by a query:

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
and delete a record with the value of the current record.

The table where I want to delete the records is called
, this table have a column called
, for example:

Assuming the table variable
have 3 records:

  • OT-001-16

  • OT-002-16

  • OT-005-16

I need to implement a kind of while loop, to get each record on the table
and for each record delete on the table
@workorders (id_workorder VARCHAR(100))(Current record)

Answer Source

Can you not simply INNER JOIN the @workorders table in your delete statement?

FROM workorders w INNER JOIN @workorders tw ON w.id_workorder = tw.id_workorder
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download