tags:

views:

18

answers:

1

When I use FOR XML PATH('') in the following query, the role values come back fine but for each result the FirstName value is <Unknown(-16)>. Here is the query:

SELECT role, STUFF((SELECT ','+FirstName FROM Employees WHERE role = 'Engineer' FOR XML PATH('')),1,1,'') AS names FROM Employees GROUP BY role

I have tried to find this error code online but have had no luck, has anyone seen this error before in SQL Server and knows how to resolve it?

Update: Just to clarify, if I reduce my query to just this I can still reproduce the error:

SELECT ','+FirstName FROM Employees WHERE role = 'Engineer' FOR XML PATH('')

So this may be an environment issue. I am on SQL Server 2005 SP2. Also note that I am running the query via JDBC (not sure if this matters).

A: 

I tried this but didn't get any error (SQL Server 2005 Developer x64):

DECLARE @Employees TABLE
  (
   Firstname nvarchar(50),
   ROLE nvarchar(50)
  ) ;

INSERT @Employees (Firstname, ROLE)
  VALUES ('Andreas', 'Engineer') ;
INSERT @Employees (Firstname, ROLE)
  VALUES ('Michael', 'Engineer') ;

SELECT role, STUFF((
                    SELECT ','+FirstName
                      FROM @Employees
                      WHERE role = 'Engineer'
                   FOR
                    XML PATH('')
                   ), 1, 1, '') AS names
  FROM @Employees
  GROUP BY ROLE ;

It correctly returns the following:

role             names
---------------- -------------------
Engineer         Andreas,Michael
Lucero
...forgot to mention it, my instance is running with SP3, so if you have no or an older service pack installed you may want to update and see if the problem persists.
Lucero
Thanks Lucero, I just updated the question with more details. I would not be surprised if this is an environment issue (I'm on SP2), just want to see if others have seen this error.
John Collins