2016-09-23 5 views
-1
SubjectID  StudentName 
----------  ------------- 
1    Mary 
1    John 
1    Sam 


SubjectID  StudentName 
----------  ------------- 
1    Mary, John, Sam 

こんにちは、私は、SQL Server 2014を使用している私はそれが1列に結果を挿入にSTUFF()関数を使用することが可能ですかどうかを知りたいと思います。私がオンラインで見ている例は、すべて取得している例です。私はそれが正しいとは思わないドキュメントに基づいてそれをやろうとしました。のSQL Server 2014を差し込みなさい機能

クエリ

@"INSERT INTO ApplicationOtherInfo 
         (ApplicationId, AppOptionCode 
         ) values 
         (@applicationId, @appCode 
         )"; 

SELECT STUFF((SELECT ',' + AppOptionCode 
       FROM ApplicationOtherInfo 
       ORDER BY AppOptionCode 
       FOR XML PATH('')), 1, 1, '') 
+2

をはい、それは次のとおりです。https:// MSDN。 microsoft.com/en-us/library/ms188043.aspx – rbr94

答えて

0

はい次のことができます。

DECLARE @Students TABLE (
    SubjectID int, 
    StudentName nvarchar(max) 
) 
INSERT INTO @Students VALUES 
(1, 'Mary'), 
(1, 'John'), 
(1, 'Sam') 

INSERT INTO SomeTable 
SELECT DISTINCT 
     s.SubjectID, 
     STUFF((SELECT ', ' + StudentName 
       FROM @Students 
       WHERE s.SubjectID = SubjectID 
       FOR XML PATH('')), 1, 2, '') 
         as StudentName 
FROM @Students s 

あなたはSomeTableから選択する場合は、取得します:

SubjectID StudentName 
1   Mary, John, Sam 
+0

試しましたか?私の解決策? – gofr1

関連する問題