473,378 Members | 1,280 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

BUG? SQL 2k - IN () allows invalid syntax and fails silently

Behavior I found:

Invalid Column name in select embedded in a IN() clause appears to
return a NULL, and fails silently - completing the query giving
incorrect results.

Behavior I expected:

RunTime error, as column didn't exist.

-------------------------------------------------------

create
table #THEONE
( RowNo int not null )

create
table #THEOTHER
( RowNumb int not null )

select *
from #THEONE
where RowNo not in (
-- SHOULD THROW RUNTIME ERROR --
select RowNo from #THEOTHER
)

-------------------------------------------------------

-- select @@VERSION

-- Microsoft SQL Server 2000 - 8.00.871 (Intel X86)
-- Oct 21 2003 16:16:50
-- Copyright (c) 1988-2003 Microsoft Corporation
-- Developer Edition on Windows NT 5.0 (Build 2195: Service Pack 4)

Jul 23 '05 #1
4 1430
nib
Actually, if I recall, there is a nuance of SQL at work here that makes
this not be a true bug. Basically, because RowNo exists in your original
table, the query will work. I don't remember all the exact specifics,
but I know I've seen it discussed.

Zach

rawheiser wrote:
Behavior I found:

Invalid Column name in select embedded in a IN() clause appears to
return a NULL, and fails silently - completing the query giving
incorrect results.

Behavior I expected:

RunTime error, as column didn't exist.

-------------------------------------------------------

create
table #THEONE
( RowNo int not null )

create
table #THEOTHER
( RowNumb int not null )

select *
from #THEONE
where RowNo not in (
-- SHOULD THROW RUNTIME ERROR --
select RowNo from #THEOTHER
)

-------------------------------------------------------

-- select @@VERSION

-- Microsoft SQL Server 2000 - 8.00.871 (Intel X86)
-- Oct 21 2003 16:16:50
-- Copyright (c) 1988-2003 Microsoft Corporation
-- Developer Edition on Windows NT 5.0 (Build 2195: Service Pack 4)

Jul 23 '05 #2
First of all, this isn't a bug. Consider this example:

create table #THEONE (
RowNo int not null,
MaxOffset int not null
)

create table #THEOTHER (
RowNumb int not null,
RowOffset int not null
)

select * from #THEONE
where RowNo not in (
select RowNo + RowOffset
-- RowNo must be a valid column name to use at this point!
from #THEOTHER
where RowOffset < MaxOffset
)

You can refer to the columns of #THEONE anywhere in the SELECT and WHERE clauses of a query from that table. If not, you'd be unable to write correlated subqueries, at the least.

To avoid this programming error, always qualify column names. Had you written this, you'd have seen the problem:
select * from #THEONE
where #THEONE.RowNo not in (
select #THEOTHER.RowNo + #THEOTHER.RowOffset
from #THEOTHER
where #THEOTHER.RowOffset < #THEONE.MaxOffset
)

This is how most programming languages work. Identifiers are visible from inside their scope, unless shadowed by a redeclaration of the same identifier in a sub-something.
Secondly, you should not be getting a NULL anywhere. The inner query of your example is select RowNo from #THEOTHER, and the the IN clause will either be IN (#THEONE.RowNo) (correlated with the #THEONE rows) or IN (), depending on whether #THEOTHER contains any rows or not.

Steve Kass
Drew University

rawheiser wrote:
Behavior I found:

Invalid Column name in select embedded in a IN() clause appears to
return a NULL, and fails silently - completing the query giving
incorrect results.

Behavior I expected:

RunTime error, as column didn't exist.

-------------------------------------------------------

create
table #THEONE
( RowNo int not null )

create
table #THEOTHER
( RowNumb int not null )

select *
from #THEONE
where RowNo not in (
-- SHOULD THROW RUNTIME ERROR --
select RowNo from #THEOTHER
)

-------------------------------------------------------

-- select @@VERSION

-- Microsoft SQL Server 2000 - 8.00.871 (Intel X86)
-- Oct 21 2003 16:16:50
-- Copyright (c) 1988-2003 Microsoft Corporation
-- Developer Edition on Windows NT 5.0 (Build 2195: Service Pack 4)

Jul 23 '05 #3
[posted and mailed, please reply in news]

rawheiser (ra*******@gmail.com) writes:
-------------------------------------------------------

create
table #THEONE
( RowNo int not null )

create
table #THEOTHER
( RowNumb int not null )

select *
from #THEONE
where RowNo not in (
-- SHOULD THROW RUNTIME ERROR --
select RowNo from #THEOTHER
)


That is perfectly legal SQL, although meaningless. This does give you a
compile-time error (not
run-time):

select *
from #THEONE a
where a.RowNo not in (
-- SHOULD THROW RUNTIME ERROR --
select b.RowNo from #THEOTHER b
)

Always, always, use column aliases for all you column references when
you have more than one table in a query!

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
Doh! ... Variable Scopeing ... I should have know that.

The obscure I can get right away, the obvious takes longer.
Thanks ...

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Tuang | last post by:
I'm checking out Python as a candidate for replacing Perl as my "Swiss Army knife" tool. The longer I can remember the syntax for performing a task, the more likely I am to use it on the spot if...
2
by: mike | last post by:
I've spent a couple of days on this and I'm to the "bang-my-head 'gainst the monitor and babble in tongues" mode. First - SuSE Pro 9.3, Linux 2.6.11.4-21.7, Apache 2.0.53, PHP 4.3.10 I have...
0
by: Winterminute | last post by:
I am trying to read a list of install programs using WMI with ASP.NET/C#. However, it fails with "Invalid Class". I have confirmed that if I query LOCALHOST then it works fine, but if I query a...
3
by: Jeff | last post by:
I was doing this: @move('non_existant_file',... and that killed php silently. Why is that fatal, and does the "@" merely suppress reporting errors? Sometimes I don't care if a move or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.