sql server - Why is this SQL Query causing an "Ambiguous column name" error? -


i have sql query fails execute:

select p.personid, createddate, * dbo.person p  join dbo.potentiallyfraudulentpeople pfp on p.personid= pfp.personid  order createddate  

the person table has pk of personid (int). potentiallyfraudulentpeople view query of person table joined other tables decide if trust person or not. potentiallyfraudulentpeople view has 1 column: personid.

when try execute query, error:

msg 209, level 16, state 1, line 3
ambiguous column name 'createddate'.

i understand error telling me createddate column name ambiguous , need preface table's alias, 'p'.

this query works:

select p.personid, createddate, * dbo.person p  join dbo.potentiallyfraudulentpeople pfp on p.personid= pfp.personid  order p.createddate  

what don't understand why need use 'p' alias in order by statement , not in select column list. also, don't understand why need use table alias @ since potentiallyfraudulentpeople view doesn't have createddate column.

can explain odd behavior?

i using sql server 2008 , ssms execute query.

update
also, tried removing createddate column select column list , query no longer requires 'p' alias in order by. query works well:

select p.personid, * dbo.person p  join dbo.potentiallyfraudulentpeople pfp on p.personid= pfp.personid  order createddate  

you selecting createddate column twice.

  1. explicitly via createddate.
  2. implicitly via *.

it doesn't know occurence want sort on - , doesn't realize both occurences refer same column.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -