高难度的Sql问题!!

我想实现类似下面语句的效果:
select * form table where key in (a,b,g,c,a,...);
详细要求:
1、in 中的列表内容有可能是重复的,也允许重复
2、要求返回结果严格按照IN 列表的顺序返回,对于重复的key内容也要返回重复的结果纪录
3、需要在一个sql语句中实现上面要求,尽量不使用临时表
[199 byte] By [mycreatedream] at [2008-5-23]
# 1
一句实现可能很难。
SQL版有(a,b,g,c,a,...)变成直排的表的例子,利用直排的表再和原表可以得出答案。
要不说说你的需求,看看有没有变通的例子。
# 2
to zqllyh(您问我也问总可以问出个所以然) :
如果要用另一个表的话,我已经做出来了。
mycreatedream at 2007-10-24 > top of Msdn China Tech,MS-SQL Server,疑难问题...
# 3
try put (a, b, c,..) in Cursor. then fectch cursor
liliang24-LL at 2007-10-24 > top of Msdn China Tech,MS-SQL Server,疑难问题...
# 4
一句没法!
declare @sql nvarchar(4000)
declare @str varchar(100)

set @str='a,b,c,d,a,b,c'

set @sql='select top 1 * from table where key='''+replace(@str,',',''' union all select top 1 * from table where key=''')+''''
exec(@sql)
j9988-j9988 at 2007-10-24 > top of Msdn China Tech,MS-SQL Server,疑难问题...
# 5
我想也只有使用union,但语句会很长。
select * form table where key =a
union all
select * form table where key =b
union all
select * form table where key =g
union all
select * form table where key =c
union all
select * form table where key =a
...
bowlder-玩石 at 2007-10-24 > top of Msdn China Tech,MS-SQL Server,疑难问题...
# 6
以下存储过成测试通过:

create proc proc_orderbykey
@p varchar(1000)
as

declare @sql varchar(8000)

set @sql='insert @temp(value) values('
select @sql=@sql+replace(@p,',',') insert @temp(value) values(')+')'
set @sql='declare @temp table (id int IDENTITY(1,1),value int)
'+@sql+'
select b.* from tablename b,@temp t
where b.key=t.value
order by t.id
'
exec (@sql)

go

调用:
exec proc_orderbykey '1,4,6,8,2,3,4,1,8,7'

这个是假设key的类型是整数的,如果是字符型,也能写,但是会多很多引号,容易出错。

# 7
谢谢各位热心朋友的回答,但是,可能我漏了一个比较重要的条件没有说,就是只能用sql语句,不能用储存过程,因为我使用Access数据库。

我想可以将问题再改变一下,能否将以下的表A :
field (text)
a,b,c,g,b,a,.....
变成表B :
field(text)
a
b
c
g
b
a
.
.
.
不知道大家能否明白我的意思?
mycreatedream at 2007-10-24 > top of Msdn China Tech,MS-SQL Server,疑难问题...
# 8
declare @str varchar(8000)
set @str='a,b,c,d'
set @str ='create table #aa (aa char(1) null) insert into #aa (aa) values(''' + REPLACE(@str,
',',''') insert into #aa (aa) values(''' ) + ''')'
exec(@str)
select * from #aa