I have the below 4 tables. I tried to do some PoC (proof of concept) on delete with join query. So I tried to delete the rows in table 't1' and 'mytable' based on the join query on 'table1' and 'table2'. The delete query with 't1' and 'mytable' are both showing unusual behaviours.
Below is the query and table definition for the four tables:
--query starts here
DELETE mytable
FROM Table2 t1
inner join
Table2 t2 on t1.Col1=t2.col1 -- the query deletes all the rows from mytable
DELETE t1
FROM Table1
inner join
Table2 on table1.Col1=table2.col1 -- the query deletes all the rows from t1
You're not giving a join predicate for the target table so your queries are interpreted as CROSS JOINs.
This:
Is equivalent to this (will have same execution plan):
And that's logically equivalent to this:
Which, if that subquery has at least one row is logically equivalent to this:
You're suppose to reference the object you're deleting from in the join.
Like:
Or, if you're using an alias: