HOME | CONTACT
 
Products 
Download 
Purchase 
Company 
Contact 
  View Shopping Cart Email Us
The danger of sub-queries on a T-SQL delete statement
 
 

How can a sub-query cause havoc on your data?

Consider this: you are doing some clean up on your database and need to delete from table [t1] all rows the id of which happens to refer to rows on a table [t2] that match a certain criteria - let's say were created before a given date. Without thinking twice you go ahead and write:

    delete from [t1]
    where id in (select id from [t2] where "some criteria")

You expect a few records will be deleted from table [t1] - you click on execute... you can't believe your eyes, SQL Server Management Studio is reporting that 200 thousand rows were deleted, oh no, the whole content of table [t1] was wiped out! How could this be? You are panicking... you realize you broke your own rules but first you want to understand how could this innocent query cause this. You examine it closer and you realize that there is no column named id on table [t2] - but shouldn't that have caused the sub-query, and consequently the whole query to fail?!

The explanation

If you try running the sub-query by itself you will see that it will fail (assuming that there is no column named id on table [t2]), but when that sub-query is part of the bigger query things behave a bit differently. The reference id in the sub-query will be resolved against table [t1] in which case it does not matter what the subquery says it is always going to return the id of the row being processed from table [t1] thus all rows from table [t1] will be deleted.

The rules

Here are a few simple rules that anyone working with data should follow religiously:

  • Always qualify the column names - had you done that your query would have failed and you would be safe
  • Never execute a delete statement without wrapping it in a transaction so that you can roll-back if you realize that you screwed up
  • Write the query as a SELECT query first - execute it, see how many and which rows will be effected and only when you are certain that the query returns the rows you want then replace the select * from with delete from

Click here to go back to the SQL Server content index page.

 
Announcements
 
Deploy multiple scripts to multiple servers with one click - Script Executor 3 released
[more...]
 
 
Products
 
xSQL Bundle
xSQL Object
xSQL Object Command Line
xSQL Data Compare
xSQL Data Command Line
xSQL Profiler
xSQL Documenter
xSQL Builder
xSQL SDK
RSS Reporter
Script Executor
Script Executor Community Edition
Object Search
 
 
 
 
 
 
Testimonials
 
WOW! What a great piece of software (xSQL Profiler) you have written. Well done!! All the best from Scotland.
- Jim B.
Sigdev Ltd., Scotland
 
xSQL Builder provides us with everything we need to automate our database processes. Thanks to this our database update process is now 6 times faster.
- Phil H.
i2i Innovate to Integrate, UK
 
This (RSS Reporter) is a very good mashup of traditional sql server management and leading-edge information access!
- Robert H.
U.S. Xpress Enterprises
 
...It also features the best (and simplest) way to move objects and data via sql script I’ve ever seen. I’m hooked.
- Steve F.
Creative Plumbing, GA
 
We have tested ... and compared it with 6 other products. We preferred xSQL Data Compare...
- Han van der H.
RIVM LTR Laboratory, The Netherlands
 
 
| Contact | Privacy Policy | Terms Of Use | Site Map |
©Copyright 2003-2007 xSQL Software. All Rights Reserved.