请参阅以下查询。
select UserStatus,UserStatus from ((Select 'Active' as UserStatus)
union (Select 'Inactive' as UserStatus))a order by UserStatus
错误:
Msg 209, Level 16, State 1, Line 3
Ambiguous column name 'UserStatus'
解决方案:
ALTER DATABASE VCarePortal
SET COMPATIBILITY_LEVEL = 100
90 = SQL Server 2005`enter code here`
100 = SQL Server 2008 and SQL Server 2008 R2
110 = SQL Server 2012.
我尝试了兼容级别模式。但我仍然会犯错误。
答案 0 :(得分:2)
请删除一个UserStatus并尝试:
select UserStatus from
(
(Select 'Active' as UserStatus)
union
(Select 'Inactive' as UserStatus)
)a order by UserStatus
答案 1 :(得分:1)
您选择了相同的列两次,删除一列并解决问题。无论如何,您不需要两次,因为每个联合的选择只返回一列。
select UserStatus from ((Select 'Active' as UserStatus)
union (Select 'Inactive' as UserStatus))a order by UserStatus
答案 2 :(得分:0)
在您订购同一列时会产生歧义,因此sql引擎无法理解需要订购哪一列。
因此,使用以下查询将解决您的问题。
-- Order by 1 means order with first column in selection
select UserStatus, UserStatus
from (
(Select 'Active' as UserStatus)
union
(Select 'Inactive' as UserStatus)
)a order by 1
-- Or remove duplicate column from selection.
select UserStatus
from (
(Select 'Active' as UserStatus)
union
(Select 'Inactive' as UserStatus)
)a order by UserStatus