Question

I've got a table which contains a number of columns one of which is an XML column. The data contained within is a point in time snapshot, in 6 months time the process is supposed to run again and query the database if a derived value contained within the XML has changed.

I've got the query working fine however I need to update the snapshotted value which will stop the query from being ran again in a further 6 months.

The table's in a similar format to this.

History Table (
HistoryID int
, RecordID int
, TypeID int
, Data xml
)

The Data column contains something like

<Data>
<Number>346</Number>
<Requery>1</Requery>
</Data>

What I need to do is update an element of Data relating to HistoryID HistoryID, Requery 346346, 1 346347, 0

If I was dealing with this in vb.Net it would be rather simple, however it seems more complicated than it should be in SQL. What's the best way of dealing with it?

Was it helpful?

Solution

I dumped my source query into a temporary table which contained an identity column.

Declare @X table ( 
ID Int identity(1,1)
, HistoryID Int
, DueDate Date
, Number Varchar(255)
, UniqueXML xml
)

I then used a while loop to temporarily store the data I want to change

Declare @m, @I as int

Select @m = Count(*), @I = 1 from @x
While @I <= @m
Begin
Declare @DueDate Date
Select @DueDate = Duedate from @x where ID = @I

                if @Date  is not null
                Begin
                Update @x
                Set  UniqueXML.modify('
                replace value of (/Date/text())[1] 
                with sql:variable("@Date")')
                Where ID = @i
                End

End

It's not a simple solution, but it works for my needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top