简体   繁体   中英

Returning all child nodes as columns from XML in Sql Server

I am doing the following to select nodes from an XML string, the first part is just to show you what I'm selecting from.

The issue is I want to do this for various different XML columns and I'd like to not have to specify the node name for each column in my select, is there a way to select all nodes as columns automatically or even a cursor using count?

DECLARE @MyXML XML
SET @MyXML = (SELECT 
            CAST (
        '<AllowAdd>N</AllowAdd>
        <Allowed>NUMSEG</Allowed>
        <AllSegmentsEqualValue>N</AllSegmentsEqualValue>
        <ClusterLevelSA>Y</ClusterLevelSA>
        <ClusterLevelPremium>Y</ClusterLevelPremium>
        <AllowAssignedAndInTrust>N</AllowAssignedAndInTrust>
        <MinSegments>1</MinSegments>
        <MaxSegments>100</MaxSegments>
        <DefaultSegments>10</DefaultSegments>
        <RoundPremiumsTo>2</RoundPremiumsTo>
        <TaxDeferredAllowance>0.05</TaxDeferredAllowance>
        <HigherTaxValueBands>HTVB</HigherTaxValueBands>
        <NumberYearsCalculationType>NONFIN</NumberYearsCalculationType>
        <OnShore>POLICY</OnShore>
        <OffShore>NONFIN</OffShore>'as XML) as x)

        SELECT 
        Data.Col.value('(/AllowAdd)[1]','Varchar(10)') as [Allow Addition of]
        ,Data.Col.value('(/Allowed)[1]','Varchar(10)') as [Allowed]
        ,Data.Col.value('(/MinSegments)[1]','Int') as [Min Segments]
        ,Data.Col.value('(/MaxSegments)[1]','Int') as [Max Segments]
        ,Data.Col.value('(/DefaultSegments)[1]','Int') as [Default Segments]
        ,Data.Col.value('(/RoundPremiumsTo)[1]','Int') as [Round Premiums To]
        ,Data.Col.value('(/AllSegmentsEqualValue)[1]','Varchar(10)') as [All Segments Equal Value]
        --,Data.Col.value('(/TaxDeferredAllowance)[1]','Varchar(10)') as [Tax Deferred Allowance]
        ,Data.Col.value('(/HigherTaxValueBands)[1]','Varchar(10)') as [Higher Tax Value Bands]
        ,Data.Col.value('(/NumberYearsCalculationType)[1]','Varchar(10)') as [Number Years Calculation Type]
        ,Data.Col.value('(/OnShore)[1]','Varchar(10)') as [OnShore]
        ,Data.Col.value('(/OffShore)[1]','Varchar(10)') as [OffShore]
        FROM @MyXML.nodes('/OffShore') AS Data(Col)

I hope, this is what you are waiting for :)

DECLARE @MyXML XML
SET @MyXML = (SELECT 
            CAST (
        '<AllowAdd>N</AllowAdd>
        <Allowed>NUMSEG</Allowed>
        <AllSegmentsEqualValue>N</AllSegmentsEqualValue>
        <ClusterLevelSA>Y</ClusterLevelSA>
        <ClusterLevelPremium>Y</ClusterLevelPremium>
        <AllowAssignedAndInTrust>N</AllowAssignedAndInTrust>
        <MinSegments>1</MinSegments>
        <MaxSegments>100</MaxSegments>
        <DefaultSegments>10</DefaultSegments>
        <RoundPremiumsTo>2</RoundPremiumsTo>
        <TaxDeferredAllowance>0.05</TaxDeferredAllowance>
        <HigherTaxValueBands>HTVB</HigherTaxValueBands>
        <NumberYearsCalculationType>NONFIN</NumberYearsCalculationType>
        <OnShore>POLICY</OnShore>
        <OffShore>NONFIN</OffShore>'as XML) as x)

DECLARE @Output nvarchar(max) = N''
DECLARE @PivotList nvarchar(max)


SELECT 
    @PivotList = COALESCE(@PivotList + ', ', N'') + N'[' + XC.value('local-name(.)', 'varchar(100)') + N']'
FROM 
    @MyXML.nodes('/*') AS XT(XC)

SET @Output = N'SELECT 
    '+@PivotList+N'
FROM
(
    SELECT 
       ColName = XC.value(''local-name(.)'', ''nvarchar(100)''),
       ColValue = ISNULL(NULLIF(CONVERT(nvarchar(max),XC.query(''./*'')),''''),XC.value(''.'',''nvarchar(max)''))
    FROM 
       @MyXML.nodes(''/*'') AS XT(XC)
) AS s
PIVOT
(
    MAX(ColValue)
    FOR ColName IN ('+@PivotList+N')
) AS t;'

EXEC sp_executesql @Output, N'@MyXml xml', @MyXML = @MyXML;

Given your input XML, you can try to use this:

SELECT 
    ColName = XC.value('local-name(.)', 'varchar(100)'),
    ColValue = xc.value('(.)[1]', 'varchar(100)')
FROM 
    @MyXML.nodes('/*') AS XT(XC)

This will output each XML element found under the root - its name and value - as a list:

在此处输入图片说明

Of course, since it's a very generic approach, you cannot really define the proper datatypes for each columns in the second xc.value() - you basically get everything as a string.

Using a cursor to build a SELECT statement and sp_executesql to execute it!

    DECLARE @MyXML XML
    DECLARE @FieldName VARCHAR(MAX)
    DECLARE @SELECT_TEXT NVARCHAR(MAX)

    SET @MyXML = (SELECT 
                    CAST (
                '<AllowAdd>N</AllowAdd>
    <Allowed>NUMSEG</Allowed>
    <AllSegmentsEqualValue>N</AllSegmentsEqualValue>
    <ClusterLevelSA>Y</ClusterLevelSA>
    <ClusterLevelPremium>Y</ClusterLevelPremium>
    <AllowAssignedAndInTrust>N</AllowAssignedAndInTrust>
    <MinSegments>1</MinSegments>
    <MaxSegments>100</MaxSegments>
    <DefaultSegments>10</DefaultSegments>
    <RoundPremiumsTo>2</RoundPremiumsTo>
    <TaxDeferredAllowance>0.05</TaxDeferredAllowance>
    <HigherTaxValueBands>HTVB</HigherTaxValueBands>
    <NumberYearsCalculationType>NONFIN</NumberYearsCalculationType>
    <OnShore>POLICY</OnShore>
    <OffShore>NONFIN</OffShore>
    '
                    as XML) as x)


    SET @SELECT_TEXT = 'SELECT '

    DECLARE xml_cursor CURSOR
        FOR SELECT Data.Col.value( 'fn:local-name(.)', 'VARCHAR(MAX)' ) FROM @MyXML.nodes('/*') AS Data(Col)

    open xml_cursor
    While 1 = 1
    BEGIN
        fetch next from xml_cursor into @FieldName

        if @@fetch_status <> 0
        begin
                break
        end

        SET @SELECT_TEXT = @SELECT_TEXT + 'Data.Col.value(''(/' + @FieldName + ')[1]'',''Varchar(MAX)'') as [' + @FieldName + ']' + ', '

    END

    close xml_cursor 
    deallocate xml_cursor 

    SET @SELECT_TEXT = SUBSTRING( @SELECT_TEXT,0, LEN(@SELECT_TEXT) ) + 'FROM @MyXML.nodes(''/OffShore'') AS Data(Col)'

    EXECUTE sp_executesql @SELECT_TEXT, N'@MyXML XML', @MyXML = @MyXML

As previously mentioned in another comment the drawback is not having column types but it's for a report style query so strings is ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM