SIGN UP MEMBER LOGIN:    
ARTICLE

Weird Thing With Inner Queries in Sql Server 2005

Posted by Virendra Dugar Articles | SQL Server 2012 February 24, 2009
This articles helps you in understand that how inner queries works with relation to outer queries and what problems it can create if not used in proper manner.
Reader Level:

Few days ago, I found a very strange thing with SQL SERVER 2005 and I strongly feel that this is a bug in Sql Server which actually can create lots of data discrepancy.

Let's go through the script.

  • Create a new fresh database.

    CREATE DATABASE DB_TEST

  • Use this Database.

    USE DB_TEST

  • Create a table name tblCategory.

    CREATE TABLE tblCategory
    (
        CategoryID INT PRIMARY KEY,
        CategoryNAME VARCHAR(50)
    )

  • Create another table named tblProduct.

    CREATE
    TABLE tblProduct
    (
        ProductID INT PRIMARY KEY,
        CategoryID INT FOREIGN KEY REFERENCES tblCategory(CategoryID),
        IsDamaged BIT
    )

  • Insert 5 rows in tblCategory.

    INSERT INTO tblCategory VALUES (1,'Category1')
    INSERT INTO tblCategory VALUES (2,'Category2')
    INSERT INTO tblCategory VALUES (3,'Category3')
    INSERT INTO tblCategory VALUES (4,'Category4')
    INSERT INTO tblCategory VALUES (5,'Category5')

  • Insert 10 rows in tblProduct.

    INSERT INTO tblProduct VALUES (1,1,0)
    INSERT INTO tblProduct VALUES (2,1,0)
    INSERT INTO tblProduct VALUES (3,2,0)
    INSERT INTO tblProduct VALUES (4,2,0)
    INSERT INTO tblProduct VALUES (5,3,0)
    INSERT INTO tblProduct VALUES (6,3,0)
    INSERT INTO tblProduct VALUES (7,4,0)
    INSERT INTO tblProduct VALUES (8,4,0)
    INSERT INTO tblProduct VALUES (9,4,0)
    INSERT INTO tblProduct VALUES (10,5,0)

  • Select statements to confirm that data is entered or not.

    SELECT * FROM tblCategory
    SELECT * FROM tblProduct

  • Here is a select query which is incorrect. The query tells that select ProductID from tblCategory where categoryId = 1, But the tblCategory table is not having a column named ProductID. So when we execute this query, it throws an error and that is the expected behaviour.

    SELECT ProductID FROM tblCategory WHERE CategoryID = 1

  • Here is the magic. I have used the above incorrect select query with a update statement as an inner query. What do you think, what should happen when you execute this query? This query should throw an error as my inner select query is not correct. But just execute this query and you will be shocked.

    UPDATE tblProduct SET IsDamaged = 1
    WHERE ProductID IN
    (SELECT ProductID FROM tblCategory WHERE CategoryID = 1)

  • Oops!!!! 10 rows affected. All the data in IsDamaged is set to 1 but my inner select query(SELECT ProductID FROM tblCategory WHERE CategoryID = 1) is wrong.

Initally I thought that this is a bug but actaully it's not. The inner query first tries to find the column in the current table (inner query's table) and if it does not found then it will look for the outer query table. It is the best practice to use the tableName.ColumnName in the inner query.

UPDATE tblProduct SET IsDamaged = 1

WHERE ProductID IN

(SELECT tblCategory.ProductID FROM tblCategory WHERE CategoryID = 1)

Now this inner query will throw an error. So next time be careful whenever you are working with inner queries.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor