2

I have got an table in SQL-Server, where I have XML stored in one column.

CREATE TABLE [dbo].[MyTable](
[MyId] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
[MyContent] [xml] NULL
)

The xml in the column "MyContent" looks like this:

<Account id="1">
    <Name>Bill</Name>
</Account>

Now I want to concatenate all columns to one big xml. I tried to do that with "for xml" function in SQL-Server:

select  
   MyContent 
from MyTable
for xml path('')

The XML, that is created looks like this:

<MyContent>
   <Account id="1">
      <Name>Bill</Name>
   </Account>
</MyContent>
<MyContent>
   <Account id="2">
      <Name>John</Name>
   </Account>
</MyContent>

But that is not what I needed, I need the XML without the created "MyContent" Tags, so what I need is this:

<Account id="1">
   <Name>Bill</Name>
</Account>
<Account id="2">
   <Name>John</Name>
</Account>

Is there any way to avoid the Tag creation for the column name?

2 Answers 2

3

Use * as column alias.

Columns with a Name Specified as a Wildcard Character

select  
   MyContent as [*]
from MyTable
for xml path('')
Sign up to request clarification or add additional context in comments.

Comments

0

Try this one -

Query:

INSERT INTO dbo.MyTable(MyContent)
VALUES 
    ('<Account id="1"><Name>Bill</Name></Account>'), 
    ('<Account id="2"><Name>Jack</Name></Account>')

SELECT [*] = MyContent 
FROM dbo.MyTable
FOR XML PATH ('')

Output:

<Account id="1">
  <Name>Bill</Name>
</Account>
<Account id="2">
  <Name>Jack</Name>
</Account>

1 Comment

Thanks, Mikael was a litle bit faster, so I will accept his answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.