-- Table with xml fragment
declare @YourTable table(SomeID int identity, YourColumn varchar(max))
-- Add 2 rows of test data
insert into @YourTable values(
'<infoElems>
<infoElem id="1" Name="somename" money="3399.3984939" />
</infoElems>')
insert into @YourTable values(
'<infoElems>
<infoElem id="1" Name="somename" money="4399.3584939" />
</infoElems>')
-- Declare a table variable with a xml column
declare @TempTable table(SomeID int, YourColumn xml)
-- Copy rows that should be modified (ID and xml is enough)
insert into @TempTable
select SomeID, YourColumn
from @YourTable
--Modify the money attribute in TempTable
;with cte as
(
select YourColumn.value('(infoElems/infoElem/@money)[1]', 'money') as MoneyCol,
YourColumn
from @TempTable
)
update cte set
YourColumn.modify('replace value of (infoElems/infoElem/@money)[1] with sql:column("MoneyCol")')
-- Write the changes back to the source table
update Y set
Y.YourColumn = cast(T.YourColumn as varchar(max))
from @YourTable as Y
inner join @TempTable as T
on Y.SomeID = T.SomeID
-- Look at the result
select *
from @YourTable
Результат:
SomeID YourColumn
------ ---------------------------------------------------------------------------
1 <infoElems><infoElem id="1" Name="somename" money="3399.3985"/></infoElems>
2 <infoElems><infoElem id="1" Name="somename" money="4399.3585"/></infoElems>
Деньги в SQL Server имеют 4 знака после запятой. Если вы хотите 2 десятичных знака, вы должны использовать вместо этого этот оператор обновления.
--Modify the money attribute in TempTable
;with cte as
(
select YourColumn.value('(infoElems/infoElem/@money)[1]', 'numeric(15,2)') as MoneyCol,
YourColumn
from @TempTable
)
update cte set
YourColumn.modify('replace value of (infoElems/infoElem/@money)[1] with sql:column("MoneyCol")')