Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

Is Ambiguous column name a SQL Server only error

I'm looking at a SQL tutorial and the command that the tutorial gave gives me an error on my SQL Server Management Studio of "Ambiguous column name". Is this error only applicable when using SQL server?

enter image description here

question from:https://stackoverflow.com/questions/65868024/is-ambiguous-column-name-a-sql-server-only-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, not at all. In reality, a column reference in SQL should always be qualified -- meaning that it should say what table it comes from. You can think of the unqualified names as a short-hand. The SQL engine does the favor of figuring out the table when it can. And when a column is in multiple tables, it cannot figure it out.

Your queries should be readable and unambiguous. In your case, your code should look something like this:

select c.cname
from college c join
     apply a
     on c.cname = a.cname
where c.enrollment > 2000 and a.major = 'CS';

Note: This is guessing where enrollment and major are coming from, because there is not enough information in your query to figure this out.

Also, this uses proper, explicit, standard, readable JOIN syntax. Never use commas in the FROM clause, even if your course/tutorial materials do so. In fact, that alone suggests that they are way out-of-date (decades old).

Also, use table aliases (the abbreviations) so queries are simpler to write and to read.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...