<i id='fRdxF'><tr id='fRdxF'><dt id='fRdxF'><q id='fRdxF'><span id='fRdxF'><b id='fRdxF'><form id='fRdxF'><ins id='fRdxF'></ins><ul id='fRdxF'></ul><sub id='fRdxF'></sub></form><legend id='fRdxF'></legend><bdo id='fRdxF'><pre id='fRdxF'><center id='fRdxF'></center></pre></bdo></b><th id='fRdxF'></th></span></q></dt></tr></i><div id='fRdxF'><tfoot id='fRdxF'></tfoot><dl id='fRdxF'><fieldset id='fRdxF'></fieldset></dl></div>
<tfoot id='fRdxF'></tfoot>

    <legend id='fRdxF'><style id='fRdxF'><dir id='fRdxF'><q id='fRdxF'></q></dir></style></legend>

      <bdo id='fRdxF'></bdo><ul id='fRdxF'></ul>

    <small id='fRdxF'></small><noframes id='fRdxF'>

      如何使用 XML 数据更新 SQL 表列

      时间:2023-06-06
      <tfoot id='DO5zK'></tfoot>

      1. <small id='DO5zK'></small><noframes id='DO5zK'>

      2. <i id='DO5zK'><tr id='DO5zK'><dt id='DO5zK'><q id='DO5zK'><span id='DO5zK'><b id='DO5zK'><form id='DO5zK'><ins id='DO5zK'></ins><ul id='DO5zK'></ul><sub id='DO5zK'></sub></form><legend id='DO5zK'></legend><bdo id='DO5zK'><pre id='DO5zK'><center id='DO5zK'></center></pre></bdo></b><th id='DO5zK'></th></span></q></dt></tr></i><div id='DO5zK'><tfoot id='DO5zK'></tfoot><dl id='DO5zK'><fieldset id='DO5zK'></fieldset></dl></div>

              <tbody id='DO5zK'></tbody>
            • <bdo id='DO5zK'></bdo><ul id='DO5zK'></ul>

                <legend id='DO5zK'><style id='DO5zK'><dir id='DO5zK'><q id='DO5zK'></q></dir></style></legend>

                本文介绍了如何使用 XML 数据更新 SQL 表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                表 1:

                id      title               chtml
                0       Lopez, Michelle MD  <root><StartOne><Value1>Lopez, Michelle MD</Value1><Value2>Spanish</Value2><Value3><a title="49 west point" href="myloc.aspx?id=56" target="_blank">49 west point</a></Value3><Value4>908-783-0909</Value4><Value5><a title="CM" href="myspec.aspx?id=78" target="_blank">CM</a></Value5></StartOne></root>
                1       Michael, Jogn, MD   <root><StartOne><Value1>Michael, Jogn, MD</Value1><Value2>English</Value2><Value3><a title="99 show drive" href="myloc.aspx?id=05" target="_blank">99 show drive</a></Value3><Value4>908-783-0909</Value4><Value5><a title="KM" href="myspec.aspx?id=40 target="_blank">KM</a></Value5></StartOne></root>
                

                chtmlntext 类型,id 0:

                The chtml is of type ntext for id 0:

                <root>
                    <StartOne>
                        <Value1>Lopez, Michelle MD</Value1>
                        <Value2>Spanish</Value2>
                        <Value3>
                            <a title="49 west point" href="myloc.aspx?id=56" target="_blank">49 west point</a>
                        </Value3>
                        <Value4>908-783-0909</Value4>
                        <Value5>
                            <a title="CM" href="myspec.aspx?id=78" target="_blank">CM</a>
                        </Value5>
                    </StartOne>
                </root>
                

                chtmlntext 类型,id 1:

                The chtml is of type ntext for id 1:

                <root>
                    <StartOne>
                        <Value1>Michael, Jogn, MD</Value1>
                        <Value2>English</Value2>
                        <Value3>
                            <a title="99 show drive" href="myloc.aspx?id=05" target="_blank">99 show drive</a>
                        </Value3>
                        <Value4>908-783-0909</Value4>
                        <Value5>
                            <a title="KM" href="myspec.aspx?id=40 target="_blank">KM</a></Value5>
                        </Value5>
                    </StartOne>
                </root>
                

                Table 2Table 3 包含用于为链接 Url、标题和值创建 Table 1 的数据:

                Table 2 and Table 3 has data which is used in the creation of Table 1 for the link Url, title, and value:

                Table 2:
                
                    id              title
                    ---------------------------------
                    56              49 west point
                    90              130 chester lane
                    12              320 nolan street
                    05              99 show drive
                
                Table 3:
                
                    id              description
                    ------------------------------
                    78              CM
                    39              IM
                    40              KM
                

                Table 4 包含更新后的数据,用于更新 Table 1 值:

                Table 4 has the updated data which will be used to update Table 1 values:

                Table 4:
                
                    Name                    Value2              Value3                  Value4              Value5
                    --------------------------------------------------------------------------------------------------------------
                    Lopez, Michelle MD      English             130 chester lane        908-783-0909        KM
                    Michael, Jogn, MD       Italian             320 nolan street        540-029-2090        IM
                

                我正在尝试使用以下查询来更新其中一个值:

                I am trying the following query to update one of the value:

                declare @xml xml;
                select @xml = cast([content_html] as xml)
                from [myDB1].[dbo].[Table 1]
                
                set @xml.modify('
                  replace value of (/root/StartOne/Value3/text())[1]
                  with "<a title="{Value3 from Table 4}" href="myloc.aspx?id={ID from Table 2 that matches the title with the title from Table 4}" target="_blank">{Value3 from Table 4}</a>"
                ');
                
                -- How can I update the anchor link values of one table by querying another table data.
                -- How can I update multiple fields, for Example `Value3` and `Value5`?
                
                update [myDB1].[dbo].[Table 1]
                set [content_html] = cast(@xml as nvarchar(max))
                where [content_title] = 'Lopez, Michelle MD'
                

                一个例子是:

                declare @xml xml;
                select @xml = cast([content_html] as xml)
                from [myDB1].[dbo].[Table 1]
                
                set @xml.modify('
                  replace value of (/root/StartOne/Value3/text())[1]
                  with "<a title="130 chester lane" href="myloc.aspx?id=90" target="_blank">130 chester lane</a>"
                ');
                
                update [myDB1].[dbo].[Table 1]
                set [content_html] = cast(@xml as nvarchar(max))
                where [content_title] = 'Lopez, Michelle MD'
                

                表一(以上更新后):

                id      title               chtml
                0       Lopez, Michelle MD  <root><StartOne><Value1>Lopez, Michelle MD</Value1><Value2>Spanish</Value2><Value3><a title="130 chester lane" href="myloc.aspx?id=90" target="_blank">130 chester lane</a></Value3><Value4>908-783-0909</Value4><Value5><a title="CM" href="myspec.aspx?id=78" target="_blank">CM</a></Value5></StartOne></root>
                1       Michael, Jogn, MD   <root><StartOne><Value1>Michael, Jogn, MD</Value1><Value2>English</Value2><Value3><a title="99 show drive" href="myloc.aspx?id=05" target="_blank">99 show drive</a></Value3><Value4>908-783-0909</Value4><Value5><a title="KM" href="myspec.aspx?id=40 target="_blank">KM</a></Value5></StartOne></root>
                

                请帮助我解决以下问题:

                Please help me with the following:

                • 如何通过查询更新一张表的锚链接值另一个表数据?
                • 如何更新多个字段,例如 Value3Value5然后运行更新语句?
                • How can I update the anchor link values of one table by querying another table data?
                • How can I update multiple fields, for Example Value3 and Value5 and then run the update statement?

                推荐答案

                这是解决问题的方法

                declare @table4 table
                (Name nvarchar(22), Value3 nvarchar(22))
                
                    insert into @table4 values ('Lopez, Michelle MD' ,'130 chester lane')       
                    insert into @table4 values ('Michael, Jogn, MD ','320 nolan street') 
                
                declare @table1 table
                (id int, title nvarchar(max), chtml ntext)
                
                
                insert into @table1 values (0,'Lopez, Michelle MD',  '<root><StartOne><Value1>Lopez, Michelle MD</Value1><Value2>Spanish</Value2><Value3>
                <a title="49 west point" href="myloc.aspx?id=56" target="_blank">49 west point</a></Value3><Value4>908-783-0909</Value4><Value5><a title="CM" href="myspec.aspx?id=78" target="_blank">CM</a></Value5></StartOne></root>')
                insert into @table1 values (1,'Michael, Jogn, MD',   '<root><StartOne><Value1>Michael, Jogn, MD</Value1><Value2>English</Value2><Value3><a title="99 show drive" href="myloc.aspx?id=05" target="_blank">99 show drive</a></Value3><Value4>908-783-0909</Value4><Value5><a title="KM" href="myspec.aspx?id=40 target="_blank">KM</a></Value5></StartOne></root>')
                
                
                declare @xml xml;
                select top 1 @xml = cast(chtml as xml)
                from @table1
                
                -- How can I update the anchor link values of one table by querying another table data.
                declare @titl nvarchar(22)
                select @titl = Value3 from @table4 where Name = 'Lopez, Michelle MD'
                set @xml.modify('
                  replace value of (/root/StartOne/Value3/a/@title)[1]
                  with sql:variable("@titl")
                ');
                set @xml.modify('
                  replace value of (/root/StartOne/Value3/a/text())[1]
                  with sql:variable("@titl")
                ');
                
                -- How can I update multiple fields, for Example `Value3` and `Value5`?
                -- Answer: here you can modify Value5
                
                update @table1
                set chtml = cast(@xml as nvarchar(max))
                where id = 0
                
                select * from @table1
                

                这篇关于如何使用 XML 数据更新 SQL 表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何将 XML 查询结果保存到文件 下一篇:对 XML 中的节点值求和时 SQL Server 的奇怪行为

                相关文章

                最新文章

                <tfoot id='Csll5'></tfoot>
                <i id='Csll5'><tr id='Csll5'><dt id='Csll5'><q id='Csll5'><span id='Csll5'><b id='Csll5'><form id='Csll5'><ins id='Csll5'></ins><ul id='Csll5'></ul><sub id='Csll5'></sub></form><legend id='Csll5'></legend><bdo id='Csll5'><pre id='Csll5'><center id='Csll5'></center></pre></bdo></b><th id='Csll5'></th></span></q></dt></tr></i><div id='Csll5'><tfoot id='Csll5'></tfoot><dl id='Csll5'><fieldset id='Csll5'></fieldset></dl></div>
                  <bdo id='Csll5'></bdo><ul id='Csll5'></ul>

                  1. <small id='Csll5'></small><noframes id='Csll5'>

                  2. <legend id='Csll5'><style id='Csll5'><dir id='Csll5'><q id='Csll5'></q></dir></style></legend>