calling a udf in listing of values in a select
i have a list of items i am selecting and want to also include a few values from a UDF mixed in.
I am pulling names of people in various roles of a project management system.
where there is a name i want to get its initials, so i want to use the Abbreviate
UDF mixed in the select to fn_ProjectStakeholders
such that it will return names and initials along side names as its result.
see the section:
ExecutiveChampion NVARCHAR(500), -- Abbreviate (ExecutiveChampion) as ExecutiveChampionInit, BusinessOwner NVARCHAR(500), -- Abbreviate (BusinessOwner) as BusinessOwnerInit,
here is my code:
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开发者_JAVA百科 @Index = CHARINDEX(' ', @InputString, @Index) + 1
END
RETURN @OutputString
END
answer inspired by this question
my resulting code is thus:
select
--p.parentprojectid,
pp.ProjectName as ParentProjectName,
p.ProjectName as ProjectName,
p.ClarityId,
R.Name as releaseName,
CASE WHEN PSH.GeneralContractor = 'Jeff Jablonski' THEN 'Y' ELSE 'N' END as 'GC',
-- cg initials
PSH.GeneralContractor,
dbo.Abbreviate(PSH.GeneralContractor),
p.CaseManagerBenId,
P.Budget,
PSH.BusinessOwner,
PSH.DevelopmentLead ,
PSH.PrimaryPM,
PSH.DevelopmentManager,
-- SA ?!?!!?
scs.CodeName as latestStatus
-- 6x true/ false status for link types (with sanity check)
from project p
left outer join project pp on pp.projectid = p.parentprojectid
inner join Release R on R.ReleaseID = P.ReleaseID
LEFT OUTER JOIN ProjectStatus ps ON ps.ProjectId = p.ProjectId
AND ps.LastUpdate = (SELECT MAX(LastUpdate)
FROM ProjectStatus ips
WHERE ips.ProjectId = p.ProjectId)
LEFT OUTER JOIN StatusCode scs ON scs.CID = ps.RAGStatusCID
Left OUTER JOIN fn_ProjectStakeholders ('25,66,97') as PSH ON PSH.projectId = p.ProjectId
where p.projectId in (25,66,97)
精彩评论