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
 General SQL Server Forums
 New to SQL Server Programming
 how can i get data as i give order

Author  Topic 

boreddy
Posting Yak Master

172 Posts

Posted - 2008-08-27 : 03:24:19
I have a table called emp with ID's 1 to 20

I wrote a Query to get the emp details 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.



madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-27 : 03:26:22
Duplicate of http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=109561

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-27 : 03:28:12
quote:
Originally posted by madhivanan

Duplicate of http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=109561

Madhivanan

Failing to plan is Planning to fail


The dup thread is gone. So we will stick to this one


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

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-27 : 03:30:54
declare @orders table(sort_order int identity(1,1) empid int)
insert into @orders(empid)
select 3 as empidunion all
select 16 union all
select 19 union all
select 2 union all
select 15union all
select 4 union all
select 7 union all
select 1

select e1.* from emp as e1 inner join @orders as e2 on e1.empid=e2.empid
order by e2.sort_order


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-27 : 03:36:26
are you using SQL Server 2000 or 2005 ?


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

Go to Top of Page
   

- Advertisement -