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. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

share|improve this answer

Your Answer


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.