Link to home
Start Free TrialLog in
Avatar of johnkainn
johnkainn

asked on

ORDER BY items must appear in the select list if SELECT DISTINCT is specified

Hello,
I have following code in sql:
SET @myCol = STUFF(( SELECT DISTINCT
               '],[' + ltrim(cast(AId as varchar(10)))
               FROM dbo.temp
               ORDER BY '],[' + ltrim(cast(AId as varchar(10)))
               FOR XML PATH('') ), 1, 2, '') + ']'

I get the error: ORDER BY items must appear in the select list if SELECT DISTINCT is specified
Do you know how to fix this?
ASKER CERTIFIED SOLUTION
Avatar of DcpKing
DcpKing
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Éric Moreau
or just use "order by 1"