5

I have a CTE:

;WITH DeleteTarget AS
(
    ....
)

How do I use this CTE for two delete statements - maybe like:

DELETE FROM [TableA]
WHERE ColumnA IN (SELECT Id FROM DeleteTarget)

DELETE FROM [TableB]
WHERE ColumnB IN (SELECT Name FROM DeleteTarget)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Max
  • 4,439
  • 2
  • 18
  • 32

1 Answers1

4

You cannot - a CTE only exists for the one, next statement.

If you need this information that the CTE provides more than once, you need to:

  • store the result set into a table variable or temp table
  • then execute your multiple statements using that table variable / temp table
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459