1

I have this T-SQL script:

DECLARE @idoc int

DECLARE @doc nvarchar(200)
SET @doc ='<ArrayOfString>
            <string>AL</string>
            <string>DZ</string>
        </ArrayOfString>'

    EXEC sp_xml_preparedocument @idoc OUTPUT, @doc;

    select *  
    FROM OPENXML (@idoc, '/ArrayOfString',2)
    WITH (string varchar(50))
    EXEC sp_xml_removedocument @idoc

This is a stored procedure. I send to it some serialized xml (in this variant I declare it as hard code).

I want to get all <string> element's values. In this variant it must be: AL and DZ, but I get only 'AL'. What is it incorrect in my script?

2 Answers 2

5
declare @xml xml
set @xml = '<ArrayOfString 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <string>Dev_1</string>
    <string>Dev_3</string>
</ArrayOfString>'

SELECT 
    T.C.value('.', 'VARCHAR(10)')
from @xml.nodes('/ArrayOfString/string') T(C)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks.It's variant work good! But, What is 'T.C.value' and T(C) in your code?
"T(C)" is alias for "@xml.nodes('/ArrayOfString/string')" in the last statement.
Good! But if it is alias, why we write 'T.C.value'? Sorry, if its silly question! :)
It is used fetch the value or content from XML node.
3

If you want to use OPENXML try this:

DECLARE @idoc int

DECLARE @doc nvarchar(200)
SET @doc ='<ArrayOfString>
            <string>AL</string>
            <string>DZ</string>
        </ArrayOfString>'

EXEC sp_xml_preparedocument @idoc OUTPUT, @doc;

select *  
    FROM OPENXML (@idoc, '/ArrayOfString/string',2)
    WITH (string varchar(50) '.')

EXEC sp_xml_removedocument @idoc

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.