CAUTION: This SQL will DELETE all rows in your table

I came across this gem the other day. Fortunately, I ran it locally first.

There are two tables. Person and AlienPerson. Lets create the tables and seed with some data.

Try to run a bad select statement:

Here’s the baddie. This will delete all data in the Person table:

To get this to happen, the following must be true:

1. The SELECT fieldĀ in the sub-query must be named the same as the fieldĀ in the outer WHERE clause.
2. The inner WHERE clause must reference a field in the outer table. Otherwise, the DELETE will not run.

Using:

Microsoft SQL Server 2012 – 11.0.5343.0 (X64)
May 4 2015 19:11:32
Copyright (c) Microsoft Corporation
Developer Edition (64-bit) on Windows NT 6.3 <X64> (Build 10586: )

Posted in SQL          

Leave a Reply

Your email address will not be published. Required fields are marked *