【发布时间】:2015-08-26 15:17:00
【问题描述】:
表 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>
chtml 的类型为 ntext,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>
chtml 的类型为 ntext,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 2 和 Table 3 具有用于为链接 Url、标题和值创建 Table 1 的数据:
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:
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
我正在尝试以下查询来更新其中一个值:
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'
表1(以上更新后):
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>
请帮我解决以下问题:
- 如何通过查询更新一个表的锚链接值 另一个表数据?
- 如何更新多个字段,例如
Value3和Value5然后运行更新语句?
【问题讨论】:
-
如果我们将
chtml数据类型设为XML,您的所有问题都可以解决:) -
@tinka 我能够修复它并使其工作,但
&lt;被插入为&lt;
标签: sql sql-server xml