把id串联起来
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/*--------------------------------------------------
Function:把id串联起来
Author: Robot.H
---------------------------------------------------*/
SET NOCOUNT ON
--prepare data
DECLARE @t TABLE(id int )
INSERT INTO @t select 1
UNION ALL select 2
UNION ALL select 3
UNION ALL select 4
--method1
DECLARE @str as varchar(200)
select @str=ISNULL(@str+',','')+CAST(id AS VARCHAR(200)) from @t
PRINT @str
--method2
select @str=
(select cast(id as varchar(200))+','
from @t
for xml path('')
)