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 2000 Forums
 SQL Server Development (2000)
 How could i solve this problem?

Author  Topic 

ctlqt12
Starting Member

2 Posts

Posted - 2006-10-07 : 07:58:36
Hi,

I have one table : Questions(QuestionId,Content) ( use MSSQL )
when i executed this sql : "select * from Questions where subjectId in ('Q002','Q001','Q009','Q007')"

i got these : ( it seems they were ordered by QuestionId automatically )

Q001 Content_1
Q002 Content_2
Q007 Content_7
Q009 Content_9


But i don't want that. I want they will be showed like this : ( with no order by QuestionId or something ).

Q002 Content_2
Q001 Content_1
Q009 Content_9
Q007 Content_7


How could i solve that?

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-10-07 : 08:07:49
Why you want to do like this?? try to order the data in the front end while displyaing it.

from query you have to do like this which is not at all recommonded.

you can not predict the order of the query, unless you have specified order by clause explicitly in your query

select * from Questions where subjectId = 'Q002'
Union All
select * from Questions where subjectId = 'Q001'
Union All
select * from Questions where subjectId = 'Q009'
Union All
select * from Questions where subjectId = 'Q007'


Chirag
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-10-07 : 08:19:03
[code]
create table #Questions
(
QuestionId varchar(10),
Content varchar(10)
)
insert into #Questions
select 'Q001', 'Content_1' union all
select 'Q002', 'Content_2' union all
select 'Q007', 'Content_7' union all
select 'Q009', 'Content_9'

select *
from #Questions

/* RESULT
QuestionId Content
---------- ----------
Q001 Content_1
Q002 Content_2
Q007 Content_7
Q009 Content_9
*/


select *
from #Questions
where QuestionId in ('Q002','Q001','Q009','Q007')
order by charindex(QuestionId, 'Q002,Q001,Q009,Q007')

/* RESULT
QuestionId Content
---------- ----------
Q002 Content_2
Q001 Content_1
Q009 Content_9
Q007 Content_7
*/


drop table #Questions
[/code]


KH

Go to Top of Page
   

- Advertisement -