2017-04-23 11 views

答えて

2

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でインヴェーダージムを見ている...たくさんの叫び声。 –

+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) 
関連する問題