日期:2014-05-17 浏览次数:20428 次
CREATE TABLE Test1([编号1] CHAR(4), [编号2] CHAR(4), [编号3] CHAR(4), [名称] varchar(20), [数量] int, [重量] varchar(20))
INSERT test1 SELECT '0001' ,'0002' ,'0003' ,'苹果', 1 ,'5公斤'
UNION ALL SELECT '0001' ,'0002' ,'0003' ,'橘子', 2 ,'6公斤'
UNION ALL SELECT '0001' ,'0002' ,'0004' ,'香蕉', 3 ,'10公斤'
UNION ALL SELECT '0001' ,'0002' ,'0004' ,'猕猴桃', 3 ,'11公斤'
GO
CREATE FUNCTION fun_Test1(@b1 CHAR(4),@b2 CHAR(4),@b3 CHAR(4))
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE @sqlT VARCHAR(200)
SELECT @sqlT=ISNULL(@sqlT+',','')+[名称]+'/'+ltrim(数量)+'/'+重量
FROM test1
WHERE 编号1=@b1 and 编号2=@b2 and 编号3= @b3
ORDER BY 编号1
RETURN @sqlT
END
GO
SELECT 编号1,编号2,编号3, dbo.fun_test1(编号1,编号2,编号3) 合并列 FROM test1 group by 编号1,编号2,编号3
--result
/*
编号1 编号2 编号3 合并列