需要将XML数据映射到SQL表格吗?

huangapple go评论83阅读模式
英文:

Have to map XML data into a SQL table?

问题

我正在我的存储过程中获取XML数据,并希望通过映射值来更新相应的SQL Server表格。

这是XML的示例:

DECLARE @xmlData XML = '<NewDataSet>
      <Table>
        <SystemKey>India</SystemKey>
        <Translation>Bharat</Translation>
      </Table>
      <Table>
        <SystemKey>UAE</SystemKey>
        <Translation>United Arab Emirates</Translation>
      </Table>
      <Table>
        <SystemKey>Afghanistan</SystemKey>
        <Translation>Afghanistan</Translation>
      </Table>
      </NewDataSet>';

代码如下:

SELECT 
    t.c.value('(SystemKey/text())[1]', 'NVARCHAR(MAX)') AS [SystemKey],
    r.c.value('text()[1]', 'NVARCHAR(MAX)') AS Translation
FROM 
    @xmlData.nodes('NewDataSet/Table') AS t(c)
CROSS APPLY 
    t.c.nodes('*[local-name(.) != "SystemKey"]') AS r(c)

我想要使用XML数据的值直接更新表格2;例如,SystemKey = Table2.KEY,并且Translation = Table2.Translation。

我可以直接做到这一点,而不需要使用临时表和游标吗?

英文:

I'm getting XML data in my stored procedure and want to update the corresponding SQL Server table by mapping the values.

This is a sample of the XML:

DECLARE @xmlData XML = &#39;&lt;NewDataSet&gt;
      &lt;Table&gt;
        &lt;SystemKey&gt;India&lt;/SystemKey&gt;
        &lt;Translation&gt;Bharat&lt;/Translation&gt;
      &lt;/Table&gt;
      &lt;Table&gt;
        &lt;SystemKey&gt;UAE&lt;/SystemKey&gt;
        &lt;Translation&gt;United Arab Emirates&lt;/Translation&gt;
      &lt;/Table&gt;
      &lt;Table&gt;
        &lt;SystemKey&gt;Afghanistan&lt;/SystemKey&gt;
        &lt;Translation&gt;Afghanistan&lt;/Translation&gt;
      &lt;/Table&gt;
      &lt;/NewDataSet&gt;&#39;

Code is as follows:

SELECT 
    t.c.value(&#39;(SystemKey/text())[1]&#39;, &#39;NVARCHAR(MAX)&#39;) AS [SystemKey],
    r.c.value(&#39;text()[1]&#39;, &#39;NVARCHAR(MAX)&#39;) AS Translation
FROM 
    @xmldata.nodes(&#39;NewDataSet/Table&#39;) AS t(c)
CROSS APPLY 
    t.c.nodes(&#39;*[local-name(.) != &quot;SystemKey&quot;]&#39;) AS r(c)

I want to update table 2 with the values from XML data; eg. SystemKey = Table2.KEY AND Translation = Table2.Translation.

Can I do this directly without a temp table and a cursor?

答案1

得分: 0

这是您想要的吗?(我不确定您尝试实现的更新类型是什么。如果您想要插入或更新,您可以使用合并操作来完成相同的操作)。

DECLARE @xmlData XML = '<NewDataSet>
      <Table>
        <SystemKey>India</SystemKey>
        <Translation>Bharat</Translation>
      </Table>
      <Table>
        <SystemKey>UAE</SystemKey>
        <Translation>United Arab Emirates</Translation>
      </Table>
      <Table>
        <SystemKey>Afghanistan</SystemKey>
        <Translation>Afghanistan</Translation>
      </Table>
      </NewDataSet>';

WITH Translations
AS
(
    SELECT 
        t.c.value('(SystemKey/text())[1]', 'NVARCHAR(MAX)') AS [SystemKey],
        r.c.value('text()[1]', 'NVARCHAR(MAX)') AS Translation
    FROM 
        @xmlData.nodes('NewDataSet/Table') AS t(c)
    CROSS APPLY 
        t.c.nodes('*[local-name(.) != "SystemKey"]') AS r(c)
)
UPDATE t2 
    SET t2.Translation = t.Translation 
FROM Table2 AS t2
INNER JOIN Translations AS t
ON t.SystemKey = t2.SystemKey;
英文:

Is this what you're after? (I wasn't sure what type of update you were trying to achieve. You could do the same with a merge if you want an insert or update).

DECLARE @xmlData XML = &#39;&lt;NewDataSet&gt;
      &lt;Table&gt;
        &lt;SystemKey&gt;India&lt;/SystemKey&gt;
        &lt;Translation&gt;Bharat&lt;/Translation&gt;
      &lt;/Table&gt;
      &lt;Table&gt;
        &lt;SystemKey&gt;UAE&lt;/SystemKey&gt;
        &lt;Translation&gt;United Arab Emirates&lt;/Translation&gt;
      &lt;/Table&gt;
      &lt;Table&gt;
        &lt;SystemKey&gt;Afghanistan&lt;/SystemKey&gt;
        &lt;Translation&gt;Afghanistan&lt;/Translation&gt;
      &lt;/Table&gt;
      &lt;/NewDataSet&gt;&#39;;

WITH Translations
AS
(
    SELECT 
        t.c.value(&#39;(SystemKey/text())[1]&#39;, &#39;NVARCHAR(MAX)&#39;) AS [SystemKey],
        r.c.value(&#39;text()[1]&#39;, &#39;NVARCHAR(MAX)&#39;) AS Translation
    FROM 
        @xmldata.nodes(&#39;NewDataSet/Table&#39;) AS t(c)
    CROSS APPLY 
        t.c.nodes(&#39;*[local-name(.) != &quot;SystemKey&quot;]&#39;) AS r(c)
)
UPDATE t2 
    SET t2.Translation = t.Translation 
FROM Table2 AS t2
INNER JOIN Translations AS t
ON t.SystemKey = t2.SystemKey;

huangapple
  • 本文由 发表于 2020年1月6日 19:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定