Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Bug with IN?

$
0
0
Hello,I've run into what seems like a bug in SQL Server.If I use an IN clause with a SELECT statement where the inner query uses a field name that doesn't exist, but DOES exist in the outer query, the statement will succeed by returning "true" for all rows rather than returning an error.Note that if a field name is used that exists in neither the inner query nor the outer query, an error [i]is[/i] raised, and the statement is terminated with "Msg 207, Level 16, State 1 Invalid column name 'the column name'." as I would have expected.The below script demonstrates the issue. When you run this script, all rows are deleted from #Test_A and SQL Server returns no error. Instead of succeeding (and deleting all rows!!) I would have expected the DELETE command to [i]fail[/i] with 'Msg 207, Level 16, State 1 Invalid column name 'ID'.Does anyone know if this has been reported to Connect previously? It is happening in SQL 2008 and SQL 2014. Is this the correct behavior for some reason? If it sounds like this is really a bug, I'll report it to Connect. Sorry if this has already been reported - I did look around and didn't find anything.Thanks very much![code="sql"]BEGIN TRANCREATE TABLE #TEST_A ( ID INT NOT NULL PRIMARY KEY, VALUE VARCHAR(50) NOT NULL)CREATE TABLE #TEST_B ( OTHERID INT NOT NULL PRIMARY KEY, VALUE VARCHAR(50) NOT NULL)INSERT INTO #TEST_A (ID, VALUE) VALUES (1,'Test One');INSERT INTO #TEST_A (ID, VALUE) VALUES (2,'Test Two');INSERT INTO #TEST_B (OtherID, VALUE) VALUES (1,'Some other test one');-- Would expect this to throw an error because there is no field named ID in #TEST_B!!!DELETE FROM #TEST_A WHERE ID IN (SELECT ID FROM #TEST_B)-- This code will correctly return an error because YetAnotherID is in neither of #TEST_A or #TEST_B-- DELETE FROM #TEST_A WHERE ID IN (SELECT YetAnotherID FROM #TEST_B)SELECT * FROM #TEST_ASELECT * FROM #TEST_B/*DROP TABLE #TEST_ADROP TABLE #TEST_B*/ROLLBACK[/code]

Viewing all articles
Browse latest Browse all 3145

Trending Articles