Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Slight Confusion Over FOR XML PATH

Author  Topic 

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-02-10 : 05:47:31
Hi all,

It's quite rare that I have to concatenate rows into a string but when I do I usually do a sub-select with a FOR XML PATH('') clause.

I thought about trying to turn those sub-selects into a nice OUTER APPLY but can't seem to get over a slight hurdle -- that the xml column returned doesn't have a name.

Here's my test set. Any advice would be appreciated.

IF OBJECT_ID('tempDb..#employee') IS NOT NULL DROP TABLE #employee
IF OBJECT_ID('tempDb..#roles') IS NOT NULL DROP TABLE #roles
IF OBJECT_ID('tempDb..#employeeRoles') IS NOT NULL DROP TABLE #employeeRoles

CREATE TABLE #employee (
[employeeId] INT PRIMARY KEY
, [name] VARCHAR(255)
)

CREATE TABLE #roles (
[roleId] INT PRIMARY KEY
, [roleName] VARCHAR(255)
)

CREATE TABLE #employeeRoles (
[employeeID] INT -- FOREIGN KEY
, [roleID] INT -- FOREIGN KEY

PRIMARY KEY([employeeID], [roleID])
)

/* Data */
INSERT #employee ([employeeID], [name])
SELECT 1, 'Fred Flintstone'
UNION SELECT 2, 'Barney Rubble'

INSERT #roles ([roleID], [roleName])
SELECT 1, 'Saftey Inspector'
UNION SELECT 2, 'Handy Man'
UNION SELECT 3, 'Plumber'

INSERT #employeeRoles ([employeeID], [roleID])
SELECT 1, 1 -- Fred (Saftey)
UNION SELECT 1, 3 -- Fred (Plumber)
UNION SELECT 2, 2 -- Barney (Handy Man)


/* FOR XML SUB_SELECT */
SELECT
[employee]
, LEFT([roles], LEN([roles]) -1) AS [Roles]
FROM
(
SELECT
e.[name] AS [employee]
, [Roles] = (
SELECT [rolename] + ', '
FROM
#employeeRoles er
JOIN #roles r ON r.[roleID] = er.[roleID]
WHERE
er.[employeeID] = e.[employeeID]
FOR XML PATH('')
)
FROM
#employee e
)
rep

/* Attempt at OUTER APPLY */
SELECT
e.[name]
, rl.[roles]
FROM
#employee e

OUTER APPLY (
SELECT [rolename] + ', ' AS [ROLES]
FROM
#employeeRoles er
JOIN #roles r ON r.[roleID] = er.[roleID]
WHERE
er.[employeeID] = e.[employeeID]
FOR XML PATH('')
)
rl



I always get the error Msg 8155, Level 16, State 2, Line 76
No column name was specified for column 1 of 'rl'.



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-10 : 05:59:38
it should be


SELECT
e.[name]
, LEFT(rl.col,LEN(rl.col)-1)
FROM
#employee e

OUTER APPLY (
SELECT [rolename] + ', ' AS [ROLES]
FROM
#employeeRoles er
JOIN #roles r ON r.[roleID] = er.[roleID]
WHERE
er.[employeeID] = e.[employeeID]
FOR XML PATH('')
)
rl(col)




------------------------------------------------------------------------------------------------------
SQL Server MVP
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-02-10 : 06:10:12
Cheers Visakh16. I could feel that I was *almost* there.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-10 : 06:12:40
No problem
You're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
Go to Top of Page
   

- Advertisement -