Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Sql Query (Select Stmt) Problem

Author  Topic 

haribabu.rk
Starting Member

2 Posts

Posted - 2008-08-30 : 00:30:39
Hi

I wrote a Query like
select * from emp where empid IN (3,16,19,2,15,4,7,1.......)

which produces the Output in the sorted order like 1,2,3,4,7,15,16,19.....

But I want the Output as in the order which I've mentioned after 'IN' operator.

Actually I am sending a Comma Separated Values of Id's string from my front end to SqlServer and appending that string after 'IN' operator.


Is there any way to get the output as I needed?

Regards,
Hari


tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-30 : 00:54:30
Your order appears to be meaningless. You'll need to insert the CSV list into a temporary table and put an identity column on it. You could then do this:

SELECT *
FROM emp e
INNER JOIN #temp t
ON e.empid = t.empid
ORDER BY t.TempId

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-30 : 00:55:51
[code]
select e.*
from emp e
inner join dbo.fnParseList(',', '3,16,19,2,15,4,7,1') l
on e.empid = l.Data
order by l.RowID

using fnParseList from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033&searchterms=fnParseList
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -