Update to XML Column
hi - newbie @ xml coluns in sql 2008 ...
i have xml column user that looks this:
<isirdatavalues xmlns="http://schemas.datacontract.org/2004/07/etc" xmlns:i="http://www.w3.org/2001/xmlschema-instance">
<valuelist>
<isirvaluetype>
<isircode>aai</isircode>
<isirvalue />
</isirvaluetype>
<isirvaluetype>
<isircode>activedutymilitary</isircode>
<isirvalue />
</isirvaluetype>
<isirvaluetype>
<isircode>transactionreceiptdate</isircode>
<isirvalue>20120215</isirvalue>
</isirvaluetype>
</valuelist>
</isirdatavalues>
i want write sql statement update 1 of values ... ie: isirvalue isircode of transactionreceiptdate ...
i have seen samples on web can't them work using [columnname].modify(replace value of ...)
could please write quick update statement case me can see i'm doing wrong??
thanks!!!!
try this:
set nocount on declare @yourtable table ( rowid int primary key, yourxml xml ) insert @yourtable ( rowid, yourxml ) select 1, '<isirdatavalues xmlns="http://schemas.datacontract.org/2004/07/etc" xmlns:i="http://www.w3.org/2001/xmlschema-instance"> <valuelist> <isirvaluetype> <isircode>aai</isircode> <isirvalue /> </isirvaluetype> <isirvaluetype> <isircode>activedutymilitary</isircode> <isirvalue /> </isirvaluetype> <isirvaluetype> <isircode>transactionreceiptdate</isircode> <isirvalue>20120215</isirvalue> </isirvaluetype> </valuelist> </isirdatavalues>' declare @newvalue varchar(20) set @newvalue = '20120401' ;with xmlnamespaces( default 'http://schemas.datacontract.org/2004/07/etc' ) select 'before' s, yourxml.query('isirdatavalues/valuelist/isirvaluetype[isircode[.="transactionreceiptdate"]]/isirvalue') @yourtable ;with xmlnamespaces( default 'http://schemas.datacontract.org/2004/07/etc' ) update @yourtable set yourxml.modify('replace value of (isirdatavalues/valuelist/isirvaluetype[isircode[.="transactionreceiptdate"]]/isirvalue/text())[1] sql:variable("@newvalue")') rowid = 1 ;with xmlnamespaces( default 'http://schemas.datacontract.org/2004/07/etc' ) select 'after ' s, yourxml.query('isirdatavalues/valuelist/isirvaluetype[isircode[.="transactionreceiptdate"]]/isirvalue') @yourtable
SQL Server > SQL Server XML
Comments
Post a Comment