You are here: Re: Subquery with invalid column name runs « MsSQL Server « IT news, forums, messages
Re: Subquery with invalid column name runs

Posted by Roy Harvey (SQL Server MVP) on 10/24/07 15:23

On Wed, 24 Oct 2007 14:42:23 -0000, Anne <nienna.gaia@gmail.com>
wrote:

>Here is the statement in question:
>--STATEMENT A
> SELECT * FROM dbo.myTable WHERE colX in (SELECT colX FROM
>dbo.sourceTable)
>
>The problem with Statement A is that 'colX' does not exist in
>'dbo.sourceTable'. It does, however, certainly exist in 'dbo.myTable'.
>Breaking the statement down, we have:

>Any idea what SQL Server is doing in Statement A?!?!

In the subquery you can refer to any column in dbo.myTable or
dbo.sourceTable, they are both "available". Since the reference to
colx does not have a qualifier, and there is no colX in
dbo.sourceTable, the one from dbo.myTable is used. Had the column
existed in both tables, SQL Server would, by design, use the one from
table defined in the subquery for any un-qualified reference.

The best way to prevent this sort of thing is to always qualify the
column references in the subquery.

Roy Harvey
Beacon Falls, CT

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация