November 30, 2009 at 7:43 am
Just wanted to make sure that if I did something like the following that I would only be deleting from the 'FROM' (or 'a') table and that it would not delete from the joined tables as well.
DELETE a FROM ATable a
INNER JOIN BTable b
INNER JOIN CTable c
WHERE b.Field = 'something'
AND c.Field = 'something else'
November 30, 2009 at 8:15 am
A delete can only affect a single table. It will affect the table that's specified straight after the DELETE [FROM]. Any tables after the second FROM are just used to identify rows to be deleted.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 30, 2009 at 8:21 am
What Gail has said is right, it would delete from only one table.
Your statement is missing ON clauses, is this only a test statement you have posted?
Blog -- LearnSQLWithBru
Join on Facebook Page Facebook.comLearnSQLWithBru
Twitter -- BruMedishetty
November 30, 2009 at 8:47 am
As Bru mentioned, you haven't joined TableA, so you might end up deleting everything in that table. Only one table is affected, but how it is affected depends on your query.
Write this as a SELECT, double check the results, and then turn it into a DELETE.
November 30, 2009 at 8:58 am
I personally nest all update/delete statements to real data in transactions as well, just to be extra cautious.
something like:
BEGIN TRAN
--Your Delete Statement
ROLLBACK TRAN
--COMMIT TRAN
November 30, 2009 at 9:04 am
Steve Jones - Editor (11/30/2009)
As Bru mentioned, you haven't joined TableA
It is joined, just the ON clauses are missing
DELETE FROM a -- table to be deleted from
FROM ATable a -- join to other tables to filter
INNER JOIN BTable b .....
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 30, 2009 at 9:08 am
Steve Jones - Editor (11/30/2009)
Write this as a SELECT, double check the results, and then turn it into a DELETE.
That's my best suggestion to a beginner and intermediate programmer. That one additional step is a way of checking / confirming what records your DELETE statement is going to delete.
Blog -- LearnSQLWithBru
Join on Facebook Page Facebook.comLearnSQLWithBru
Twitter -- BruMedishetty
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply