Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2012-08-28
2
Medium Priority
?
745 Views
Last Modified: 2012-08-29
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?
0
Comment
Question by:johnkainn
2 Comments
 
LVL 16

Accepted Solution

by:
DcpKing earned 2000 total points
ID: 38343905
Change
ORDER BY '],[' + ltrim(cast(AId as varchar(10)))
into
ORDER BY AId

hth

Mike
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38344845
or just use "order by 1"
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question