2017-04-23 116 views

回答

2

使用stuff() with select ... for xml path ('') method of string concatenation

create table t (ProductId int); 
insert into t values (68) ,(74) ,(58) ,(64) ,(67); 

select 
    ProductIds = stuff((
     select ','+convert(varchar(10),ProductId) 
     from t 
     for xml path (''), type).value('.','nvarchar(max)') 
     ,1,1,'') 

rextester演示:http://rextester.com/RZQF31435

回報:

+----------------+ 
| ProductIds | 
+----------------+ 
| 68,74,58,64,67 | 
+----------------+ 
+0

在DVD上看着侵略者Zim ......很多人大叫。 –

+0

@JohnCappelletti我通過保留我的sql小寫來平衡它^^^ – SqlZim

1

您可以使用串聯和選擇數據的東西,XML路徑從第一個逗號後..

select distinct stuff((select ',' + convert(char(2), productid) from #yourproductid for xml path('')),1,1, '') 
    from #yourproductid 

你表:

create table #yourproductid(productid int) 

insert into #yourproductid (productid) values (68),(74),(58),(64),(67) 
相關問題