SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


tsql for XML format the values as elements


tsql for XML format the values as elements

Author
Message
isingh
isingh
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 59
I am trying to get the values in a column as XML elements. Is it possible to do this using For XML in sql server?

declare @XMLTest table( [Name] [nvarchar](50) NOT NULL )

INSERT @XMLTest ([Name]) VALUES (N'One¬d¦Uº')
INSERT @XMLTest ([Name]) VALUES (N'Two')
INSERT @XMLTest([Name]) VALUES (N'Three')
I would like to get the following on separate rows from the select query.

This would help me escape the Invalid characters in the values, so they can then be serialized to XML properly.

<One_x00AC_d_x00A6_U_x00BA_>
<Two/>
<Three/>
Is it possible to get this return from the FOR XML query, so that the invalid characters are properly encoded when the sql server generates the XML
?
LutzM
LutzM
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24171 Visits: 13559
I'm not sure why those character wouldn't be properly displayed.
I tried
select NAME as a from @XMLTest for XML PATH('')


and received
<a>One¬d¦Uº</a>
<a>Two</a>
<a>Three</a>

So I don't think there's any need to replace thoses special character.
What exact output are you looking for (especially in terms of tag names)?



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
isingh
isingh
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 59
Thanks for your reply Lutz, but as you can see in my post, I want the output to be

<One_x00AC_d_x00A6_U_x00BA_>
<Two/>
<Three/>

so that each element in the column data be represented as an XML Element and not text in the element.
LutzM
LutzM
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24171 Visits: 13559
I don't understand why values of a column would need to be translated into tag (or element) names without any text value, especially if the values contain invalid character for xml elements. But there might be reasons out there to do so...

I'm out.



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
sestell1
sestell1
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3562 Visits: 3511
Here's a horrible and almost completely useless solution, but it does work... although it doesn't return the elements in the shortened notation....


DECLARE @XMLTest TABLE( [Name] [NVarChar](50) NOT NULL )
DECLARE @SQL AS VarChar(500)

INSERT @XMLTest ([Name]) VALUES (N'One¬d¦Uº')
INSERT @XMLTest ([Name]) VALUES (N'Two')
INSERT @XMLTest([Name]) VALUES (N'Three')

SELECT
@SQL = 'SELECT [' +
Max(CASE Ranking WHEN 1 THEN Name ELSE Null END) + '] = '''', [' +
Max(CASE Ranking WHEN 2 THEN Name ELSE Null END) + '] = '''', [' +
Max(CASE Ranking WHEN 3 THEN Name ELSE Null END) + '] = ''''' +
'FOR XML RAW(''''), ELEMENTS'
FROM
(
SELECT
ROW_NUMBER() OVER(ORDER BY Name) AS Ranking,
Name
FROM
@XMLTest
) X

EXEC (@SQL)


isingh
isingh
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 59
thanks, this will do the job
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search