当前位置: 代码迷 >> Sql Server >> 求存储过程SQL存储临时ID值.高手快看解决方法
  详细解决方案

求存储过程SQL存储临时ID值.高手快看解决方法

热度:78   发布时间:2016-04-27 16:20:40.0
求存储过程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值,[email protected],不能使用游标和临时表。效率要高
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 @[email protected]+ ', '+rtrim(ID) from test where BODY= 'D '
select @id=stuff(@id,1,1, ' ')
print @id

drop table test
  相关解决方案