简体   繁体   English

基于存储在SQL Server XML列中的数据构建字符串

[英]Build String Based on Data Stored in SQL Server XML Column

I've been attempting to write this query but, so far, to no avail. 我一直在尝试编写此查询,但到目前为止没有任何结果。

The following is some partial data from an XML column in SQL Server: 以下是SQL Server中XML列中的部分数据:

<DashboardWidgets>
    <DashboardWidget id="14">
        <EnumName>PersonalProgressIndividual</EnumName>
        <OnOff>1</OnOff>
        <Movable>0</Movable>
        <Removable>0</Removable>
        <SubItemData>
            <SubItem id="1">
                <OnOff>1</OnOff>
                <Movable>1</Movable>
                <Removable>1</Removable>
            </SubItem>
            <SubItem id="2">
                <OnOff>1</OnOff>
                <Movable>1</Movable>
                <Removable>1</Removable>
            </SubItem>
            <SubItem id="3">
                <OnOff>1</OnOff>
                <Movable>1</Movable>
                <Removable>1</Removable>
            </SubItem>
            <SubItem id="4">
                <OnOff>0</OnOff>
                <Movable>0</Movable>
                <Removable>0</Removable>
            </SubItem>
            <SubItem id="6">
                <OnOff>0</OnOff>
                <Movable>0</Movable>
                <Removable>0</Removable>
            </SubItem>
        </SubItemData>
    </DashboardWidget>
</DashboardWidgets>

My goal is to query the table and retrieve a formatted string of IDs and values. 我的目标是查询表并检索ID和值的格式化字符串。

For an example, I would need to query the DashboardWidget node with the ID of 14 and build the string from the SubItemData child nodes contained within. 例如,我将需要查询ID为14的DashboardWidget节点,并从其中包含的SubItemData子节点构建字符串。

The string result required for a query of the dashboard widget with the ID of 14 would be: 查询ID为14的仪表板小部件所需的字符串结果为:

"1,1,1,1|2,1,1,1|3,1,1,1|4,0,0,0|6,0,0,0"

I've been able to come close by extracting all the values but without any delimiters at all. 我已经能够通过提取所有值来接近目标,但根本没有任何定界符。

DECLARE
    @companyID  INT = 23
    ,@dwID      INT = 14
;

DECLARE @xml xml
SELECT @xml = c.DashboardWidgetSettings FROM dbo.Company c WHERE c.CompanyID = @companyID;

SELECT
    x.Rec.query('./SubItem').value('.', 'varchar(max)') AS 'SubItemData'
FROM @xml.nodes('/DashboardWidgets/DashboardWidget[@id=sql:variable("@dwID")]/SubItemData') as x(Rec)
;

Any help or a point in the right direction would be greatly appreciated. 任何帮助或朝着正确方向的观点将不胜感激。

Declare @XML xml = '<DashboardWidgets><DashboardWidget id="14"><EnumName>PersonalProgressIndividual</EnumName><OnOff>1</OnOff><Movable>0</Movable><Removable>0</Removable><SubItemData><SubItem id="1"><OnOff>1</OnOff><Movable>1</Movable><Removable>1</Removable></SubItem><SubItem id="2"><OnOff>1</OnOff><Movable>1</Movable><Removable>1</Removable></SubItem><SubItem id="3"><OnOff>1</OnOff><Movable>1</Movable><Removable>1</Removable></SubItem><SubItem id="4"><OnOff>0</OnOff><Movable>0</Movable><Removable>0</Removable></SubItem><SubItem id="6"><OnOff>0</OnOff><Movable>0</Movable><Removable>0</Removable></SubItem></SubItemData></DashboardWidget></DashboardWidgets>'

Select Stuff((Select Distinct '|' +String 
              From (
                    Select String = f.n.value('@id','varchar(50)') 
                                   +','
                                   +f.n.value('(OnOff)[1]','varchar(50)') 
                                   +','
                                   +f.n.value('(Movable)[1]','varchar(50)') 
                                   +','
                                   +f.n.value('(Removable)[1]','varchar(50)') 
                     From  @XML.nodes('DashboardWidgets/DashboardWidget/SubItemData') t(n)
                     Cross Apply t.n.nodes('SubItem ') f(n)
                   ) X
              For XML Path ('')),1,1,'')

Returns 退货

1,1,1,1|2,1,1,1|3,1,1,1|4,0,0,0|6,0,0,0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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