テーブルのSQLServerXMLデータ列にいくつかのデータが格納されています。これは、HTMLコンテンツ(電子メールとして送信される)を構成するために使用され、正常に機能します。ただし、一部のデータを変更したいのですが、必要なビットだけを実行するための正しい呪文を見つけることができないようです。
例として以下を取り上げます。
declare @s1 varchar(max);
declare @s2 varchar(max);
declare @s3 varchar(max);
-- rough approximation of my data
set @s1 = '<email sender="bob"><html><body><table><tbody><tr><td><b>'
set @s2 = '</b></td></tr><tr><td><b>'
set @s3 = '</b></td></tr></tbody></table></body></html></email>'
declare @t table (id int, data xml)
insert into @t values (1, @s1 + 'Hello World' + @s2 + 'Goodbye cruel world' + @s3)
insert into @t values (2, @s1 + 'Hello World' + @s2 + 'I''m leaving you today' + @s3)
insert into @t values (3, @s1 + 'Hello World' + @s2 + 'Goodbye, goodbye, goodbye' + @s3)
select data from @t
update @t -- change sender to "fred"
set data.modify('replace value of (/email/@sender)[1] with "fred"')
select data from @t
select data,x.nd.value('b[1]','varchar(max)') -- find the "hello world" bits
from @t
cross apply data.nodes('email/html/body/table/tbody/tr/td') as x(nd)
where x.nd.value('b[1]','varchar(max)') = 'Hello World'
「送信者」データ(私が解決したと思う)と「HelloWorld」データを変更したいのですが、変更方法がわかりません。「crossapply」構文で何が起こっているのかよくわかりません。XPathのものを記述して、「where」句のない「HelloWorld」ノードのみを見つけることができると思いますか?
私のテーブルには約9行しかないため、必要に応じて9つの更新コマンドを記述できます。これは1回限りのデータ保守タスクです。
どんな助けでもいただければ幸いです。
編集:
これはその方法ですか?
update @t
set data.modify('replace value of (email/html/body/table/tbody/tr/td/b/text())[1] with "bonjour monde"')
from @t
cross apply data.nodes('email/html/body/table/tbody/tr/td') as x(nd)
where x.nd.value('b[1]','varchar(max)') = 'Hello World'
ありがとう。