This is my root element. I wrote a stored procedure to i...">

XQuery Insert without SQL2008 Namespace

<game xmlns="http://my.name.space" ></game>  

This is my root element. I wrote a stored procedure to insert elements into it. To summarize the stored procedure, here is SQL

UPDATE ChessGame SET GameHistory.modify('insert <move><player>black</player><piece>pawn</piece><start>E7</start><end>E6</end></move> as last into (/game)[0]') WHERE Id = @GameId;

Now when MSSQL does the insertion, an empty namespace is also inserted, so the result is

<move xmlns="">
  <player>black</player>
  <piece>king</piece>
  <start>E7</start>
  <end>E6</end>
</move>

Now I tried to use both

WITH XMLNAMESPACES(DEFAULT 'http://my.name.space')

and

GameHistory.modify('declare default element namespace "http://my.name.space"; insert ...')

But in the end, I get prefixes everywhere and a namespace declaration for each element.

, MSSQL. xml ( ?). , , , , ?

+5
2
declare @x xml;
select @x='<game xmlns="http://my.name.space" ></game>';
set @x.modify('declare default element namespace "http://my.name.space"; 
    insert <move><player>black</player><piece>pawn</piece>
     <start>E7</start><end>E6</end></move> as last into (/game)[1]');
select @x;

:

<game xmlns="http://my.name.space">
  <move>
    <player>black</player>
    <piece>pawn</piece>
    <start>E7</start>
    <end>E6</end>
  </move>
</game>

SQL 2005 SP2, SQL 2008 SP1.

:

declare @t table (x xml);
insert into @t (x) values ('<game xmlns="http://my.name.space" ></game>');
update @t
set x.modify('declare default element namespace "http://my.name.space"; 
    insert <move><player>black</player><piece>pawn</piece>
       <start>E7</start><end>E6</end></move> as last into (/game)[1]');
select * from @t;
+2

, :

DECLARE @x XML;
SET @x = '<game xmlns="http://my.name.space" ></game>';

select @x

SET @x.modify(
    ' declare default element namespace "http://my.name.space";
    insert <move><player>black</player><piece>pawn</piece><start>E7</start><end>E6</end></move> as last into (/*:game)[1]'
    )

select @x
+3

All Articles