SQL Server 2014插入填充函数
您好,我正在使用SQL Server 2014。我想知道是否可以使用STUFF()函数将结果插入到一列中。我在网上看到的例子都是检索。我试着做它的基础上的文件,它似乎是不正确的 查询SQL Server 2014插入填充函数,sql,sql-server,Sql,Sql Server,您好,我正在使用SQL Server 2014。我想知道是否可以使用STUFF()函数将结果插入到一列中。我在网上看到的例子都是检索。我试着做它的基础上的文件,它似乎是不正确的 查询 SubjectID StudentName ---------- ------------- 1 Mary 1 John 1 Sam SubjectID StudentName --------
SubjectID StudentName
---------- -------------
1 Mary
1 John
1 Sam
SubjectID StudentName
---------- -------------
1 Mary, John, Sam
是的,你可以:
@"INSERT INTO ApplicationOtherInfo
(ApplicationId, AppOptionCode
) values
(@applicationId, @appCode
)";
SELECT STUFF((SELECT ',' + AppOptionCode
FROM ApplicationOtherInfo
ORDER BY AppOptionCode
FOR XML PATH('')), 1, 1, '')
如果从SomeTable
中选择,您将获得:
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
是的,你试过我的解决方案吗?
SubjectID StudentName
1 Mary, John, Sam