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.
- explicitly via
createddate
. - implicitly via
*
.
it doesn't know occurence want sort on - , doesn't realize both occurences refer same column.
Comments
Post a Comment