日期:2014-05-17 浏览次数:20667 次
--sql2000中只能用自定义的函数
IF OBJECT_ID('dbo.fn_merger', 'FN') IS NOT NULL
DROP FUNCTION dbo.fn_merger
GO
CREATE FUNCTION fn_merger(@billid VARCHAR(255), @mxitemno INT)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @rv NVARCHAR(MAX)
SET @rv = N''
SELECT @rv = @rv + N'、' + LTRIM(unpassquantity)+unpassremark FROM dbo.pqualitydetail2 WHERE billid = @billid AND mxitemno = @mxitemno
RETURN STUFF(@rv,1,1,'')
END
GO
--sql:
SELECT
a.billid,a.billcode,a.billdate,a.traderid,
b.materialid,
b.mxitemno,
b.quantity,
dbo.fn_merger(c.billid, c.mxitemno)
FROM pquality a
INNER JOIN pqualitydetail b
ON a.billid = b.billid
INNER JOIN pqualitydetail2 c
ON b.billid = c.billid
AND b.mxitemno = c.mxitemno
GROUP BY
a.billid,a.billcode,a.billdate,a.traderid,
b.materialid,
b.mxitemno,
b.quantity