简体   繁体   English

SQL Select 语句中的歧义列错误

[英]Ambiguous Column Error In SQL Select Statement

SELECT IB,* 
FROM SaleOrder 
WHERE IB IS NOT NULL 
ORDER BY IB

Error :错误 :

Msg 209, Level 16, State 1, Line 1 Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'IB'.不明确的列名“IB”。

Can somebody please explain why am I getting error while executing above SQL statement in SQL Server 2012 whereas same runs fine in SQL Server 2008?有人可以解释为什么我在 SQL Server 2012 中执行上述 SQL 语句时出错,而在 SQL Server 2008 中运行良好?

My guess is your SQL Server 2008 database is in SQL Server 2000 compatibility mode, because normally it should return the same error as your 2012 instance.我的猜测是您的 SQL Server 2008 数据库处于 SQL Server 2000 兼容模式,因为通常它应该返回与您的 2012 实例相同的错误

Try fully qualifying the table name in your query and running it in SQL Server 2008 in the context of a database with the default compatibility level (eg in the context of tempdb ), and you will likely see the error.尝试完全限定查询中的表名并在具有默认兼容级别的数据库上下文中(例如在tempdb上下文中)在 SQL Server 2008 中运行它,您可能会看到错误。

The difference in behaviour is by design and is documented in this Technet article as follows (emphasis added):行为上的差异是设计使然,并在此 Technet 文章中记录如下(强调):

Compatibility-level setting of 80兼容级别设置为 80

When binding the column references in the ORDER BY list to the columns defined in the SELECT list, column ambiguities are ignored and column prefixes are sometimes ignored.将 ORDER BY 列表中的列引用绑定到 SELECT 列表中定义的列时,列歧义会被忽略,列前缀有时也会被忽略。 This can cause the result set to return in an unexpected order.这可能会导致结果集以意外的顺序返回。

Your table already contains column IBID so in ORDER BY clause sql server is not able to decide which column to access and sort by , IBID from table obtained in * or IBIS column specified at beginning in SELECT list您的表已包含列IBID,因此在ORDER BY子句中,sql server 无法决定要访问和排序的列、从 * 中获得的表中的IBID或在SELECT列表开头指定的 IBIS 列

Select IBID,s.* from SaleHeader s WHERE IBID IS NOT NULL Order BY s.IBID

specify an alias to table as shown above指定表的别名,如上所示

Select * from SaleHeader WHERE IBID IS NOT NULL Order BY IBID

That is because the column IBID occurs twice in your results now.这是因为IBID列现在在您的结果中出现了两次。

You should either add an alias or remove the column.您应该添加别名或删除该列。

So either this:所以要么这样:

Select * from SaleHeader WHERE IBID IS NOT NULL Order BY IBID

Or:或者:

Select s.IBID colX,* from SaleHeader s WHERE s.IBID IS NOT NULL Order BY s.IBID

About the 'why':关于“为什么”:

Is the query exact the same?查询是否完全相同? Maybe the interpreter is different on both platforms.也许两个平台上的解释器不同。 Is there a difference in your use of this query?您对这个查询的使用有什么不同吗? (for example, do you run this separately or as a view definition. that differs a lot in how it is analyzed) (例如,您是单独运行它还是作为视图定义运行。这在分析方式上有很大不同)

That is because you are selecting IBID column twice.那是因为您选择了IBID列两次。

Try:尝试:

Select * from SaleHeader WHERE IBID IS NOT NULL Order BY IBID

your table have already that column.您的表已经有该列。 if you use 2 times column in one query than sql server need to specify which table column want to access.如果在一个查询中使用 2 次列,则 sql server 需要指定要访问的表列。 use tablename.column name in order by.按顺序使用tablename.column名称。

SELECT IBID,* 
FROM SaleHeader 
WHERE IBID IS NOT NULL 
ORDER BY SaleHeader.IBID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM