I am having some difficulty writing some SQL Server script to remove a few rows from two databases. I have seen quite a few other questions on the topic but nothing that appears to work for this scenario. I apologize in advance for such a basic question but this is a query which a cannot afford to screw up. I query a lot of data but I seldom make writes to tables.
I have two tables rollinfo
and defects
which are linked by
defects.roll_id = rollinfo.roll_idx
There is a column in the rollinfo
table called rollinfo.num_defects
.
If that value is greater than @MAX_DEFECTS
, I want to delete it. In addition I want to remove any corresponding rows from the defects
table.
Selecting this information is easy:
SELECT
D.ROLL_ID,
R.ROLL_IDX
FROM
VISION17SLITTER.DBO.ROLLINFO R
INNER JOIN
VISION17SLITTER.DBO.DEFECTS D ON D.ROLL_ID = R.ROLL_IDX
WHERE
R.NUM_DEFECTS > @MAX_DEFECTS
However I wasn't sure if rows could be deleted from two tables at once by just changing the select statement to delete or if I need to do some type of "where exists" statement.
For the record, this will be about 200k rows, so if there is more than one way to do this, I would like to know which is more efficient.
Thanks,
Dan