Search code examples
sqlsql-serverfor-xml-path

Is there any way to order the result set by what you want in SQL Server?


Is there any way to select from SQL Server by 'Queue, serie ...'.

For example I want to get some rows by using identifier.

I want to get rows ordered by like C, D, A, F

SELECT * 
FROM BRANCH 
WHERE IDENTIFIER IN ('C', 'D', 'A', 'F')

And this query turns rows order by random.

Maybe ordered as

  • 'F', 'D', 'A', 'C'

  • 'A', 'B', 'C', 'D'

How can I get the result set ordered as 'C', 'D', 'A', 'F'? I need this using for for xml path usage.


Solution

  • SELECT b.*
    FROM dbo.BRANCH b
    JOIN (
        VALUES
            (1, 'C'),
            (2, 'D'),
            (3, 'A'),
            (4, 'F')
    ) c(ID, IDENTIFIER) ON c.IDENTIFIER = b.IDENTIFIER
    ORDER BY c.ID