views:

20

answers:

1

Hello,
How can I update the value of an xml tag with the value of an xml tag from another related table?

something like this:

UPDATE v2
 SET
 [xml].modify ('replace value of (//TAG1/text())[1] 
                with "CAST(v1.[xml].query(''//TAG2'') AS NVARCHAR(MAX))"')
FROM 
 table2 v2, 
 table1 v1 
WHERE
 v2.id = v1.id

Thanks

A: 

I don't think you can do this in a single step - but you can do it in two steps, if you're on SQL Server 2008:

DECLARE @NewValue NVARCHAR(50)

SELECT @NewValue = [xml].value('(//TAG2)[1]', 'NVARCHAR(50)')
FROM dbo.v1 
WHERE id = 1

UPDATE dbo.v2
SET [xml].modify('replace value of (//TAG1/text())[1] with sql:variable("@NewValue")')
WHERE id = 1

The ability to specify a sql:variable in your replace value of XQuery is a new feature in SQL Server 2008 - so if you're stuck on 2005, this won't work, unfortunately.

marc_s
is there any way to make it work with "sql:variable"?
halfjust
@halfjust: yes - see the method I showed. But I don't see any way to do a "mass update" - you need a **variable** to be able to use `sql:variable` - you cannot have just another XQuery expression....
marc_s