大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说SqlServer使用STUFF拼接内容[通俗易懂],希望您对编程的造诣更进一步.
–插入一下临时数据源
with m as(
select “张三” name,”语文”course,”89″score union all
select “张三” name,”数学”course,”100″score union all
select “张三” name,”英语”course,”40″score union all
select “张三” name,”物理”course,”93″score union all
select “张三” name,”地理”course,”95″score union all
select “张三” name,”化学”course,”80″score union all
select “李四” name,”语文”course,”89″score union all
select “李四” name,”数学”course,”100″score union all
select “李四” name,”英语”course,”40″score union all
select “李四” name,”物理”course,”93″score union all
select “李四” name,”地理”course,”95″score union all
select “李四” name,”化学”course,”80″score
)select * into #info from m
select * from #info
–STUFF 拼接
select distinct name,
STUFF((
SELECT “|” + course+”=”+score+”分”
FROM #info B
WHERE B.name=A.name FOR XML PATH(“”)), 1, 1, “”) “拼接串”
from #info A
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/5921.html