2

Xml Column returns no rows.
Below created a table type and xml value inserted into that table. After executing the below mentioned two queries no value is returned.
Xml data is a valid data.

DECLARE @Test TABLE (Id INT IDENTITY (1,1), XMLDATA XML)

INSERT INTO @test
SELECT '
<TXLife xmlns="http://ACORD.org/Standards/Life/2" xmlns:tx="http://ACORD.org/Standards/Life/2" Version="2.20.00">
    <TXLifeRequest PrimaryObjectID="Holding_1">
        <CorrelationGUID>4b30545a-158b-441a-a37a-0b259f757059</CorrelationGUID>
    </TXLifeRequest>
</TXLife>'

SELECT 
      Id
    , XMLDATA.query('//CorrelationGUID') AS 'TransType'
    , XMLDATA
FROM @test

SELECT C.value('./CorrelationGUID[1]', 'varchar(50)') AS 'TransType'
FROM @test
CROSS APPLY XMLDATA.nodes('/TXLife/TXLifeRequest') n (C)

3 Answers 3

1

Well, if I test your code with CREATE TABLE, here and here, the unfiltered select works just fine. However, the second filtered select returns no results.

If I alter the query to to specify the right namespace, like in @Devart's answer,

WITH XMLNAMESPACES (DEFAULT 'http://ACORD.org/Standards/Life/2')
SELECT
        C.value('./CorrelationGUID[1]', 'varchar(50)') AS 'TransType'
    FROM
        Test
    CROSS APPLY
        XMLDATA.nodes('/TXLife/TXLifeRequest') n (C)

You can see, it works as expected.

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

1 Comment

TransType is empty.
1

You need to declare the namespace. Try this:

SELECT
  XMLDATA.value('declare namespace L="http://ACORD.org/Standards/Life/2";(//L:CorrelationGUID)[1]',
                'UNIQUEIDENTIFIER') AS 'TransType'
FROM   @test

UNION ALL

SELECT
  C.value('declare namespace L="http://ACORD.org/Standards/Life/2";(./L:CorrelationGUID)[1]',
          'UNIQUEIDENTIFIER') AS 'TransType'
FROM   @test
CROSS APPLY
  XMLDATA.nodes('declare namespace L="http://ACORD.org/Standards/Life/2";/L:TXLife/L:TXLifeRequest') N(C)

See it working here.

Comments

0

Try this one -

DECLARE @XML XML
SELECT  @XML = '
<TXLife xmlns="http://ACORD.org/Standards/Life/2" xmlns:tx="http://ACORD.org/Standards/Life/2" Version="2.20.00">
    <TXLifeRequest PrimaryObjectID="Holding_1">
        <CorrelationGUID>4b30545a-158b-441a-a37a-0b259f757059</CorrelationGUID>
    </TXLifeRequest>
</TXLife>'

;WITH XMLNAMESPACES (DEFAULT 'http://ACORD.org/Standards/Life/2')
SELECT t.c.value('CorrelationGUID[1]', 'UNIQUEIDENTIFIER')
FROM @XML.nodes('//TXLifeRequest') t(c)

Output -

------------------------------------
4B30545A-158B-441A-A37A-0B259F757059

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.