Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I know the contents of @message_body are:

<inserted DocNo="xxxx99999"/>

I want to put 'DocNo' into a variable I created @DocNo and have tried various this such as:

SET @DocNo = (SELECT ref.value('@DocNo', 'nvarchar(255)')
              FROM @message_body.nodes('//Sample_Message') T(ref))

with no luck. I don't have much experience parsing XML with SQL. Any suggestions?

share|improve this question
    
There is no XML element Sample_Message. Shouldn't you be querying for @message_body.nodes('//inserted') instead? –  Remus Rusanu Jun 27 '12 at 17:09
    
Yeah probably, I'm not very experience with parsing XML using MSSQL –  J Max Jun 27 '12 at 17:14

1 Answer 1

up vote 3 down vote accepted
DECLARE @docNo nvarchar(255)
DECLARE @message_body xml = N'<inserted DocNo="12345"/>'

SELECT
    @docNo = @message_body.value('/inserted[1]/@DocNo', 'nvarchar(255)')

SELECT @docNo
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.