I need to convert rows from a SQL Server database into comma-separated values but before I do that I need to look up an id value from a lookup table. The main table holds a index value and the lookup table has the text so my Select statement looks like,
SELECT
dbo.tbProgramGrant.ProgramPercent,
dbo.tbProgramGrant.GrantFundingID,
dbo.tbProgram.Acronym
FROM
dbo.tbProgram
INNER JOIN
dbo.tbProgramGrant ON dbo.tbProgram.ProgramID = dbo.tbProgramGrant.ProgramID
If I save it as a view with the name vwProgramCode
I can create my comma separated value using:
SELECT DISTINCT
p1.GrantFundingID,
STUFF ((SELECT ', ' + RTRIM(p.Acronym) + ':' + CONVERT(nvarchar(15), p.ProgramPercent)
FROM vwProgramCode p
WHERE p.GrantFundingID = p1.GrantFundingID
FOR XML PATH('')), 1, 1, '') AS codelist
FROM
vwProgramCode p1
GROUP BY
p1.GrantFundingID
However I would prefer not to use the view but I can't get the syntax correct injecting the view Select statement in the Select stuff statement.
I was wondering if someone could point me in the right direction.
Thanks