2

I have a SQL Server 2005 database which has a table called improvements.

Within Improvements is a field called TransactionID and one called Comments. The field Comments is a XML data type with such entries as follows.

<Comment>
    <CreatedBy>Bob</CreatedBy>
    <CreatedDateTime>2009-08-24T11:36:13.020774+01:00</CreatedDateTime>
    <CreaterRole>Manager</CreaterRole>
    <CreationStage>INITIAL REVIEW</CreationStage>
    <CreationStatus>APPROVED</CreationStatus>
    <UserComment>Approved</UserComment>
</Comment>
<Comment>
    <CreatedBy>Bob</CreatedBy>
    <CreatedDateTime>2009-08-24T11:36:25.7240616+01:00</CreatedDateTime>
    <CreaterRole>Manager</CreaterRole>
    <CreationStage>CAPTURE</CreationStage>
    <CreationStatus>ACCEPTED</CreationStatus>
    <UserComment />
</Comment>

I need to enter an additional entry along the lines of:

 <Comment>
    <CreatedBy>Bob</CreatedBy>
    <CreatedDateTime>2013-01-29T11:36:25.7240616+01:00</CreatedDateTime>
    <CreaterRole>Manager</CreaterRole>
    <CreationStage>CLOSED</CreationStage>
    <CreationStatus>CLOSED</CreationStatus>
    <UserComment>Closed as agreed<UserComment />
 </Comment>

Where the TransactionID = A transaction number.

I have found a number of solutions on the internet to amend an entry using SET..... but cannot figure out how to actually add an entry.

Can anyone help me with this one please?

Many thanks in advance.

Kev.

1 Answer 1

1
declare @TransactionID int
declare @Comment xml

set @TransactionID = 1
set @Comment = '
<Comment>
    <CreatedBy>Bob</CreatedBy>
    <CreatedDateTime>2013-01-29T11:36:25.7240616+01:00</CreatedDateTime>
    <CreaterRole>Manager</CreaterRole>
    <CreationStage>CLOSED</CreationStage>
    <CreationStatus>CLOSED</CreationStatus>
    <UserComment>Closed as agreed</UserComment>
</Comment>'

update improvements 
set Comments.modify('insert sql:variable("@Comment") as last into /')
where TransactionID = @TransactionID
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.