SELECT IB,*
FROM SaleOrder
WHERE IB IS NOT NULL
ORDER BY IB
Error :
Msg 209, Level 16, State 1, Line 1
Ambiguous column name '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?
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.
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.
The difference in behaviour is by design and is documented in this Technet article as follows (emphasis added):
Compatibility-level setting of 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. 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
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.
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.
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. use tablename.column name in order by.
SELECT IBID,*
FROM SaleHeader
WHERE IBID IS NOT NULL
ORDER BY SaleHeader.IBID
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.