sql server 函数-将多条数据,在一行一列显示

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31971935/article/details/66972487

USE [ChangShaDB]
GO

/* Object: UserDefinedFunction [dbo].[GetProjectUnit] Script Date: 03/27/2017 15:48:10 */
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[GetProjectUnit](@ProjectRowGuid nvarchar(50)) RETURNS nvarchar(2000)
AS
BEGIN

DECLARE @Distance nvarchar(50)
DECLARE @CodeText nvarchar(2000)
select @CodeText=name from
(
select stuff((select ‘,’+Unit.UnitType+’:’+Unit.UnitName+’:’+Unit.UnitCode from Pro_Unit left join Unit
on Pro_Unit.UnitRowGuid=Unit.RowGuid
where ProRowGuid=@ProjectRowGuid

ORDER BY GETDATE() for xml path(”)),1,1,”) ‘name’ from Code_Items a
) as T

RETURN @CodeText
END
GO

注:@ProjectRowGuid 为另一个表中的字段名,一对多关系。

展开阅读全文

没有更多推荐了,返回首页