Query error with ambiguous column name in SQL

前端 未结 8 782
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 06:54

I get an Ambiguous column name error with this query (InvoiceID). I can\'t figure out why. They all seem to be joined correctly so why doesn\'t the management studio know to

8条回答
  •  北海茫月
    2020-11-27 07:47

    if you join 2 or more tables and they have similar names for their columns sql server wants you to qualify columns which they belong.

    SELECT  ev.[ID]
        ,[Description]
        FROM   [Events] as ev 
        LEFT JOIN  [Units] as un ON ev.UnitID = un.UnitId  
    

    if Events and Units tables has same column name (ID) SQL server wants you to use aliases.

提交回复
热议问题