I found a query miss written in one of our databases like this
Use database2
go
delete database1.dbo.table1
from table1 as t1
join table2 on t1.column1 = t2.column2 and t2.column3 = 1
I am having a hard time getting my head around it as it looks like it will delete EVERYTHING from database1.dbo.table1 if column3 in table1 or table2 = 1 and if the join condition is successful (Please correct me if I am wrong). It is not meant to even touch database1.
Also, if that happens, which database transaction log will get huge, database1 or database2? Query runs in Database2 but delete is happening for table in database1.
Thanks for your input.
Best Answer
Creating a minimal, complete, and verifiable example will help you better formulate questions in future, and allow other folks to help you more easily.
I've created one:
Looking at the tables is instructive:
Cleanup:
The takeaway appears to be that all rows in db1.dbo.table1 are deleted, which makes sense since the
FROM dbo.table1 JOIN dbo.table2 ...
clause does not referencedatabase1.dbo.table1
at all.