У меня есть список элементов, которые я выбираю, и хочу также включить несколько значений из UDF, смешанного в.
Я собираю имена людей в различных ролях системы управления проектами.
там, где есть имя, я хочу получить его инициалы, поэтому я хочу использовать Abbreviate
UDF, смешанный в select для fn_ProjectStakeholders
, чтобы он возвращал имена и инициалы вдоль имен сторон в качестве результата.
см. Раздел:
ExecutiveChampion NVARCHAR(500),
-- Abbreviate (ExecutiveChampion) as ExecutiveChampionInit,
BusinessOwner NVARCHAR(500),
-- Abbreviate (BusinessOwner) as BusinessOwnerInit,
вот мой код:
CREATE FUNCTION [dbo].[fn_ProjectStakeholders]
(
@ProjectListCSV VARCHAR(8000)
)
RETURNS @TableOfValues TABLE
(
ProjectId INT,
ExecutiveChampion NVARCHAR(500),
-- Abbreviate (ExecutiveChampion) as ExecutiveChampionInit,
BusinessOwner NVARCHAR(500),
-- Abbreviate (BusinessOwner) as BusinessOwnerInit,
BusinessAnalyst NVARCHAR(500),
GeneralContractor NVARCHAR(500),
PrimaryPM NVARCHAR(500),
DevelopmentManager NVARCHAR(500),
DevelopmentLead NVARCHAR(500),
TDM NVARCHAR(500),
PTM NVARCHAR(500)
)
AS
BEGIN
DECLARE @pList TABLE (pk INT IDENTITY(1,1),ProjectId INT)
INSERT INTO @pList (ProjectId) SELECT Value FROM Split(',', @ProjectListCSV)
INSERT INTO @TableOfValues
SELECT ProjectId,
ISNULL([95],'n/a') ExecutiveChampion,
ISNULL([96],'n/a') BusinessOwner,
ISNULL([97],'n/a') BusinessAnalyst,
ISNULL([100],'n/a') GeneralContractor,
ISNULL([101],'n/a') PrimaryPM,
ISNULL([102],'n/a') DevelopmentManager,
ISNULL([103],'n/a') DevelopmentLead,
ISNULL([104],'n/a') TDM,
ISNULL([105],'n/a') PTM
FROM (
SELECT pl.ProjectId, StakeholderCID, FullName
FROM @pList pl
INNER JOIN StatusCode sc ON 1 = 1 AND SCID IN (8, 9)
LEFT JOIN ProjectStakeholder ps ON pl.ProjectId = ps.ProjectId AND sc.CID = ps.StakeholderCID
) AS ST
PIVOT
(MAX(FullName) FOR StakeholderCID IN ([95], [96], [97], [100], [101], [102], [103], [104], [105])) AS PT
RETURN
END
CREATE FUNCTION dbo.Abbreviate ( @InputString varchar(1000) )
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @Index INT
DECLARE @OutputString VARCHAR(100)
SET @InputString = LTRIM(@InputString)
SET @OutputString = UPPER(LEFT(@InputString, 1))
SET @Index = CHARINDEX(' ', @InputString) + 1
WHILE @Index > 1
BEGIN
SET @OutputString = @OutputString + UPPER(SUBSTRING(@InputString, @Index, 1))
SET @Index = CHARINDEX(' ', @InputString, @Index) + 1
END
RETURN @OutputString
END