1

I have a @MyXMLString parameter being passed to a stored procedure in the following format:

DECLARE @MyXMLString XML
SET @MyXMLString =
'<data formID="2">
 <UnAssigned AcctTypeID="1"/><UnAssigned AcctTypeID="2"/>
 </data>'

In the stored procedure, I have to retrieve the value of formID and AcctTypeID and then update a table.

Now there will be only one formID, which I am able to retrieve

DECLARE @formID int
SET @formID = @MyXMLString.value('(data/@formID)[1]','int')

But there can be one or multiple AcctTypeID, i.e. one or more of '<UnAssigned AcctTypeID="1"/>'. I need help in figuring out how to retrieve it.

2 Answers 2

3

You can fetch all instances of AcctTypeID like this:

SELECT 
    XC.value('@AcctTypeID', 'int')
FROM
    @MyXMLString.nodes('/data/UnAssigned') AS XT(XC)

When you execute this, these are the results:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

It is returning two rows of NULL and not getting actual values of AcctTypeID.
@SilverFish: then you haven't showed us the actual XML that you're getting - with the XML you've posted, this code works .... Also: be careful, XML processing is case sensitive! Make sure to have all element and/or attribute names cased correctly !
0

This should work:

DECLARE @MyXMLString XML
SET @MyXMLString =
'<data formID="2">
 <UnAssigned AcctTypeID="1"/><UnAssigned AcctTypeID="2"/>
 </data>'

SELECT X.value('../@formID', 'int') FormID, X.value('@AcctTypeID', 'int') AcctTypeID
FROM @MyXMLString.nodes('data/UnAssigned') T(X)

Result

FormID      AcctTypeID
----------- -----------
2           1
2           2

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.