欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Sql function 多行中的列合并为一行一列的方法

程序员文章站 2023-12-16 13:48:34
复制代码 代码如下: create table tb(standards varchar(50), amount varchar(50), variation varcha...
复制代码 代码如下:

create table tb(standards varchar(50), amount varchar(50), variation varchar(50),statuss varchar(50),reason varchar(50))
insert into tb values('55','279','4','物量积压','加工人员设备不足;t排制作进度较慢;')
insert into tb values('55','279','4','物量积压','部件人员不足;')
insert into tb values('55','279','4','物量积压','跨间场地积压;图纸问题较多;')
insert into tb values('56','300','4','物量积压','aaaa;')
insert into tb values('56','300','4','物量积压','bbbb;')
insert into tb values('56','300','4','物量积压','cccc;')

create function test(@standards varchar(100))
returns varchar(8000)
as
begin
declare @re varchar(500)
set @re = ''
select @re = @re+','+reason
from tb
where @standards=standards
return (stuff(@re,1,1,''))
end

调用
复制代码 代码如下:

select distinct standards,amount,variation,statuss,reason = dbo.test('55') from tb where standards=55
select distinct standards,amount,variation,statuss,reason = dbo.test('56') from tb where standards=56

上一篇:

下一篇: