Link to home
Start Free TrialLog in
Avatar of Marcus Aurelius
Marcus AureliusFlag for United States of America

asked on

SQL Server for XML PATH giving wrong results.

Experts:

This script:

select [name] + ', ' as NameTest
from tempdb.sys.columns where object_id = object_id('tempdb..##attribute2')
for xml path('')


Results in this:
<NameTest>Column_1_Name, </NameTest><NameTest>Column_2_Name, </NameTest><NameTest>Column_3_Name, </NameTest><NameTest>Column_4_Name, </NameTest>

How do I modify the script to produce THIS:

Column_1_Name, Column_2_Name, Column_3_Name, Column_4_Name


Any assistance is greatly appreciated!
Avatar of zc2
zc2
Flag of United States of America image

I would create a scalar function for that, like:
CREATE function [dbo].[fVal_Join]( @obj_id int )
returns varchar(max)
as
begin
	declare @str varchar(max) = NULL
	SELECT @str = COALESCE(@str + ', ', '') + [name] from tempdb.sys.columns where object_id = @obj_id
	return isnull(@str,'')
end
go

Open in new window

Then call it like
select [dbo].[fVal_Join]( object_id('tempdb..##attribute2') ) for xml path('')

Open in new window

SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marcus Aurelius

ASKER

thanks