Delete records from both SQL Server tables in a JOIN using a CTE

SSingh

I want to delete records from both tables in the JOIN from the this CTE in SQL Server 2012:

WITH RECORDS_TO_DELETE ([Counter], [ID])
AS
(
SELECT D.[Counter], D.[ID], C.[ID] 
FROM [Table1] AS D
           LEFT JOIN 
               (
                 SELECT [ID] FROM [Table2]
                 WHERE NOT LOWER (COL1) = '*my criteria*'
                 AND (Date1 <= '31-Mar-2010' OR Date2 <= '31-Mar-2010')) AS C
            ON D.ID = C.ID   
)
DELETE FROM RECORDS_TO_DELETE

I have seen other examples that say its important to do this in the correct order but those examples are not in CTEs. I used a CTE because I have a convoluted WHERE clause (perhaps complicated by the fact my JOIN is to a result set) that I haven't felt necessary to include here. A CTE was the only thing that selected the data I wanted deleted.

Is there a way to delete records from both tables where there is a match between both tables?

Eric Brandt

In order to delete the records from both tables, use your CTE to identify the primary key values for all of the records in both tables that you want to delete and write those values somewhere (table variable, temp table, actual table, probably based on the volume of your data).

Then execute two DELETE statements, one against each table, joining on the key values for the table in question.

You'll need that table to hold your values since, after you delete from the first table, your CTE won't work to identify the needed records anymore.

WITH cte AS
(
    INSERT YOUR LOGIC HERE
)
INSERT INTO @KeyValuesHoldingTable
SELECT 
    *
FROM 
    cte;

DELETE Table1
FROM
    Table1 as t
JOIN
    @KeyValuesHoldingTable as h
        ON t.KeyValues = h.KeyValues;

DELETE Table2
FROM
    Table2 as t
JOIN
    @KeyValuesHoldingTable as h
        ON t.KeyValues = h.KeyValues;       

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to Delete using INNER JOIN with SQL Server?

From Dev

Delete from CTE with join

From Dev

Delete information from two join tables in MySQL using php

From Dev

DELETE FROM 2 tables with JOIN (SQL)

From Dev

Merge and UNION records from two tables using JOIN

From Dev

SQL Server : update multiple records from joined tables

From Dev

Delete multiple records from different tables with SQL stored procedure

From Dev

SQL Server 2012 - assistance with self join or CTE

From Dev

Delete orphan records using Join

From Dev

Delete from 2 tables using INNER JOIN

From Dev

SQL Server : matching records from 2 tables with sorting

From Dev

Best practices deleting many records from several tables in SQL Server

From Dev

Fetch records from multiple tables using join

From Dev

Delete rows from table using JOIN - SQL Server

From Dev

Delete from one table using join on two tables

From Dev

SQL Left Join Foreign Key Both Tables

From Dev

How to Delete Duplicate records from Table in SQL Server?

From Dev

Delete information from two join tables in MySQL using php

From Dev

Retrieve records from Multiple tables using Join

From Dev

SQL Server : update multiple records from joined tables

From Dev

MySql: delete from multiple tables using left join

From Dev

invalid records using inner join from two tables

From Dev

Delete duplicate records from SQL Server 2012 table with identity

From Dev

How to delete rows from two tables using INNER JOIN in mysql?

From Dev

Best practices deleting many records from several tables in SQL Server

From Dev

Delete both children and parent SQL records

From Dev

SQL Server: join two tables using loop

From Dev

SQL server left join not returning expected records from left table

From Dev

How to delete records from two different tables that are linked with FK? SQL

Related Related

  1. 1

    How to Delete using INNER JOIN with SQL Server?

  2. 2

    Delete from CTE with join

  3. 3

    Delete information from two join tables in MySQL using php

  4. 4

    DELETE FROM 2 tables with JOIN (SQL)

  5. 5

    Merge and UNION records from two tables using JOIN

  6. 6

    SQL Server : update multiple records from joined tables

  7. 7

    Delete multiple records from different tables with SQL stored procedure

  8. 8

    SQL Server 2012 - assistance with self join or CTE

  9. 9

    Delete orphan records using Join

  10. 10

    Delete from 2 tables using INNER JOIN

  11. 11

    SQL Server : matching records from 2 tables with sorting

  12. 12

    Best practices deleting many records from several tables in SQL Server

  13. 13

    Fetch records from multiple tables using join

  14. 14

    Delete rows from table using JOIN - SQL Server

  15. 15

    Delete from one table using join on two tables

  16. 16

    SQL Left Join Foreign Key Both Tables

  17. 17

    How to Delete Duplicate records from Table in SQL Server?

  18. 18

    Delete information from two join tables in MySQL using php

  19. 19

    Retrieve records from Multiple tables using Join

  20. 20

    SQL Server : update multiple records from joined tables

  21. 21

    MySql: delete from multiple tables using left join

  22. 22

    invalid records using inner join from two tables

  23. 23

    Delete duplicate records from SQL Server 2012 table with identity

  24. 24

    How to delete rows from two tables using INNER JOIN in mysql?

  25. 25

    Best practices deleting many records from several tables in SQL Server

  26. 26

    Delete both children and parent SQL records

  27. 27

    SQL Server: join two tables using loop

  28. 28

    SQL server left join not returning expected records from left table

  29. 29

    How to delete records from two different tables that are linked with FK? SQL

HotTag

Archive