Ambiguous Column Error In SQL Select Statement - sql

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 (e.g. 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 you are selecting IBID column twice.
Try:
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)

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

Related

Ambiguous column name for order by the selected column

I never notice below before:
SELECT A,*
FROM Table
ORDER BY A
It gives me the Ambiguous column name error. The * of course contains A, but will sql server take the same column A in above query as two different columns? Is this the reason behind that?
it is because the output has the same name if you did
SELECT A AS A_STANDS_ALONE,*
FROM Table
ORDER BY A
It will work
If you are looking for yes/no answer, then 'YES', you are right.
SQL Selects A column twice, one by explicit and one by the implicit * call.
SQL doesn't understand which one you want to sort by.
The first comment to your post shows that you can use an alias for the column.

Error show when update using phpmyadmin

When I update a column using phpmyadmin in database with following query
UPDATE members
SET `refered` = (SELECT COUNT (*)
FROM `user_details`
WHERE `user_details.sponser`=`members.username`
)
It show a error message like this
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*) FROM `user_details` WHERE `user_details.sponser`=`members.username`)' at line 1
What may be reason?
Error is in
COUNT (*)
-----^
Remove the space between COUNT and (*).
Try the below query
UPDATE members SET refered = (SELECT COUNT(*) FROM user_details
WHERE user_details.sponser=members.username)
Does the Select query returns any result. If so what is the result. Looks like all your query is inside '' single quotes that you are using it should be removed. Single quotes need to be removed for example .
UPDATE members
SET refered = (SELECT COUNT (*)
FROM user_details
WHERE user_details.sponser=members.username
)
-- there is not single quotes in the query above. please remove it from yours.
Part of your problem, or maybe the whole problem, is the WHERE clause. You've used backticks for the table name, which is correct (or, at least, it's optional in this case; it's needed if your database name or table name has a MySQL reserved name or is otherwise ambiguous). The problem, though, is that the dot separating the database from the table needs to be outside the backticks. So your WHERE clause should look like this instead:
WHERE `user_details`.`sponser`=`members`.`username`

Unable to understand query after where clause in postgresql

I have a javaFX application with postgresql as a database , I go through the code . But unable to understand sql query in where clause. here is sql query.
SELECT *
FROM gr_group
WHERE gr_parent_id = ? AND gr_id <> 0
ORDER BY gr_description
You are looking at a parametrized SQL query. The question mark ? you see in the WHERE clause will be filled with an actual parameter coming from a Java variable.
<> (Not Equal To) is an operator. It is same as != operator.
This query will select all the columns from the table gr_group where gr_id is not equal to 0 and gr_parent_id specifies a place holder which will be filled later in the code. This query also orders selected data in the ascending order of column gr_description.

Sub-Queries in Sybase SQL

We have an application which indexes data using user-written SQL statements. We place those statements within parenthesis so we can limit that query to a certain criteria. For example:
select * from (select F_Name from table_1)q where ID > 25
Though we have discovered that this format does not function using a Sybase database. Reporting a syntax error around the parenthesis. I've tried playing around on a test instance but haven't been able to find a way to achieve this result. I'm not directly involved in the development and my SQL knowledge is limited. I'm assuming the 'q' is to give the subresult an alias for the application to use.
Does Sybase have a specific syntax? If so, how could this query be adapted for it?
Thanks in advance.
Sybase ASE is case sensitive w.r.t. all identifiers and the query shall work:
as per #HannoBinder query :
select id from ... is not the same as select ID from... so make sure of the case.
Also make sure that the column ID is returned by the Q query in order to be used in where clause .
If the table and column names are in Upper case the following query shall work:
select * from (select F_NAME, ID from TABLE_1) Q where ID > 25

Using an Alias column in the where clause in ms-sql 2000

I know you cannot use a alias column in the where clause for T-SQL; however, has Microsoft provided some kind of workaround for this?
Related Questions:
Unknown Column In Where Clause
Can you use an alias in the WHERE clause in mysql?
“Invalid column name” error on SQL statement from OpenQuery results
One workaround would be to use a derived table.
For example:
select *
from
(
select a + b as aliased_column
from table
) dt
where dt.aliased_column = something.
I hope this helps.
Depending on what you are aliasing, you could turn it into a user defined function and reference that in both places. Otherwise your copying the aliased code in several places, which tends to become very ugly and means updating 3+ spots if you are also ordering on that column.