SQL Server 2008: rename an element using XML DML?

Can I use the XML DML operator to rename an element in an untyped XML column?

I am updating the collection of XML schemas in the XML column and must correct existing XML instances by renaming one element before I can apply the last schema.

As far as I can tell from the docs, you can insert / delete nodes or replace them.

+3
source share
3 answers

As the saying goes, "Where there is a way"

: -, xml xml, , . Legs/Leg Limbs/Limb, ,

, -, .

:

declare @xml as xml = '<animal species="Mouse">
  <legs>
    <leg>Front Right</leg>
    <leg>Front Left</leg>
    <leg>Back Right</leg>
    <leg>Back Left</leg>
  </legs>
</animal>'

set @xml = (select 
     t.c.value('@species', 'varchar(max)') as '@species'
    ,(select
     ti.C.value('.', 'varchar(max)') 
from @Xml.nodes('//animal/legs/leg') ti(c) for xml path('limb'), /* root('limb'), */type) as    limbs   
from @xml.nodes('//*:animal') t(c) for xml path('animal'), type)

select @xml;

while (@xml.exist('/animal/limbs/limb') = 1) begin
    /*insert..*/
    set @xml.modify('
            insert <leg>{/animal/limbs/limb[1]/text()}</leg>
            before (/animal/limbs/limb)[1]
        ');
    /*delete..*/
    set @xml.modify('delete (/animal/limbs/limb)[1]');
end

set @xml.modify('
        insert <legs>{/animal/limbs/leg}</legs>
        before (/animal/limbs)[1]
    ');
set @xml.modify('delete (/animal/limbs)[1]');

select @xml;
+2

SQL Server Unit Test (ssut - . ) xml, . , , . , <original_record_set><original_record /></original_record_set>, , <test_record_set><test_record /></ test_record_set >.

, , :

SET @output = (SELECT col1, col2
    FROM   @test_object_result
    FOR xml path ( test_record  '), root( test_record_set '));

:

SET @output = (SELECT col1, col2
    FROM   @test_object_result
    FOR xml path (  original_record'), root(  original_record_set '));

, SAME , " xml" path('...') root('...'), .

xml , node @relation_name @tuple_name. , .


, ! , . / , , , , , .

USE [unit_test];
GO
IF EXISTS  (SELECT * FROM   sys.objects  WHERE  object_id = OBJECT_ID(N'[dbo].[standardize_record_set]')   AND type IN ( N'FN', N'IF', N'TF', N'FS', N'FT' ))
  DROP FUNCTION [dbo].[standardize_record_set];
GO
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
SET nocount ON;
GO
/*
DECLARE
  @relation_name nvarchar(150)= N'standardized_record_set',
  @tuple_name    nvarchar(150)= N'standardized_record',
  @xml           xml,
  @standardized_result xml;

SET @xml='<Root>
    <row id="12" two="now1" three="thr1" four="four1" />
    <row id="232" two="now22" three="thr22" />
    <row id="233" two="now23" three="thr23" threeextra="extraattrinthree" />
    <row id="234" two="now24" three="thr24" fourextra="mealsoin four rwo big mone" />
    <row id="235" two="now25" three="thr25" />
</Root>';

execute @standardized_result =  [dbo].[standardize_record_set] @relation_name=@relation_name, @tuple_name=@tuple_name, @xml=@xml;
select @standardized_result;

*/
CREATE FUNCTION [dbo].[standardize_record_set] (@relation_name nvarchar(150)= N'record_set',
                                                @tuple_name    nvarchar(150)= N'record', @xml  xml )
returns XML
AS
  BEGIN
      DECLARE
        @attribute_index int = 1,
        @attribute_count int = 0,
        @record_set      xml = N'<' + @relation_name + ' />',
        @record_name     nvarchar(50) = @tuple_name,
        @builder         nvarchar(max),
        @record          xml,
        @next_record     xml;
      DECLARE @record_table TABLE (
        record xml );

      INSERT INTO @record_table
      SELECT t.c.query('.') AS record
      FROM   @xml.nodes('/*/*') T(c);

      DECLARE record_table_cursor CURSOR FOR
        SELECT cast([record] AS xml)
        FROM   @record_table

      OPEN record_table_cursor

      FETCH NEXT FROM record_table_cursor INTO @next_record

      WHILE @@FETCH_STATUS = 0
        BEGIN
            SET @attribute_index=1;
            SET @attribute_count = @next_record.query('count(/*[1]/@*)').value('.', 'int');
            SET @builder = N'<' + @record_name + N' ';

            -- build up attribute string
            WHILE @attribute_index <= @attribute_count
              BEGIN
                  SET @builder = @builder + @next_record.value('local-name((/*/@*[sql:variable("@attribute_index")])[1])',
                                                               'varchar(max)') + '="' + @next_record.value('((/*/@*[sql:variable("@attribute_index")])[1])',
                                                                                                           'varchar(max)') + '" ';
                  SET @attribute_index = @attribute_index + 1
              END

            -- build record and add to record_set
            SET @record = @builder + ' />';
            SET @record_set.modify('insert sql:variable("@record") into (/*)[1]');

            FETCH NEXT FROM record_table_cursor INTO @next_record
        END

      CLOSE record_table_cursor;

      DEALLOCATE record_table_cursor;

      RETURN @record_set;
  END;

GO 
0

Yes, you can use DML to rename an element by editing it in the node you want to rename by introducing a new node into that element and then pasting the cut elements back into xml on the node. Ive made a SQL script for demonstration. http://sqlfiddle.com/#!3/dc64d/1 This will change

<animal species="Mouse">
<legs>
<leg>Front Right</leg>
<leg>Front Left</leg>
<leg>Back Right</leg>
<leg>Back Left</leg>
</legs>
</animal>

at

<animal species="Mouse">
<armsandlegs>
<leg>Front Right</leg>
<leg>Front Left</leg>
<leg>Back Right</leg>
<leg>Back Left</leg>
</armsandlegs>
</animal>

SqlFiddle seems to have broken my decision long ago. From memory ive inserted the basis of my solution below ...

DECLARE @XML2 xml
DECLARE @XML3 xml = '<limbs></limbs>'
DECLARE @XML xml = 
'<animal species="Mouse">
<legs>
<leg>Front Right</leg>
<leg>Front Left</leg>
<leg>Back Right</leg>
<leg>Back Left</leg>
</legs>
</animal>'

SET @XML2 = @XML.query('animal/legs/*')

SET @XML.modify('
insert      
    (sql:variable("@XML3"))
after
    (/animal/legs)[1]
')
SET @XML.modify('
delete (/animal/legs[1])
')
SET @XML.modify('
insert      
    (sql:variable("@XML2"))
as last into
    (/animal/limbs)[1]
')
select @XML
0
source

Source: https://habr.com/ru/post/1761244/


All Articles