日期:2014-05-18  浏览次数:20731 次

求存储过程SQL存储临时ID值....高手快看
表1--table1
ID   BODY
1     A
2     D
3     D
4     DF
5     SE
6     D
7     RS
8     D
9     YR
----------------------------------
我的目的就是用一个变量可以存储刚才操作过的ID值,把符合条件的所有ID组成字符串放到@id变量里,不能使用游标和临时表。效率要高
declare   @id   as   nvarchar(1000)
setlect   @id=id   from   table   where   BODY= 'D '
期望的结果时
@id=2,3,6,7
不知道技术上是否可以实现.可以的话,帮帮我...


------解决方案--------------------

create table test(ID int,BODY varchar(10))
insert test select 1, 'A '
union all select 2, 'D '
union all select 3, 'D '
union all select 4, 'DF '
union all select 5, 'SE '
union all select 6, 'D '
union all select 7, 'RS '
union all select 8, 'D '
union all select 9, 'YR '

declare @id as nvarchar(1000)
set @id= ' '
select @id=@id+ ', '+rtrim(ID) from test where BODY= 'D '
select @id=stuff(@id,1,1, ' ')
print @id

drop table test