【问题标题】:Reading XMl blob using and inserting specific values in a column使用特定值读取 XMl blob 并在列中插入特定值
【发布时间】:2013-06-07 02:22:35
【问题描述】:

大约有 100 个这样的列。 Blob 中的一列当前看起来像:

<GridDataVisibleColumn>
                <FilterBehavior>StronglyTyped</FilterBehavior>
                <FilterBarMode>Immediate</FilterBarMode>
                <AllowFilter>false</AllowFilter>
                <AllowSort>true</AllowSort>
                <AllowDrag>false</AllowDrag>
                <AllowGroup>true</AllowGroup>
                <AllowResize>true</AllowResize>
                <ShowColumnOptions>false</ShowColumnOptions>
                <HeaderText>XRef</HeaderText>
                <IncrementSeed>1</IncrementSeed>
                <IsIdentity>false</IsIdentity>
                <IsReadOnly>false</IsReadOnly>
                <MappingName>XRef</MappingName>
                <MinimumWidth>0</MinimumWidth>
                <Width>
                  <UnitType>None</UnitType>
                  <Value>150</Value>
                </Width>
                <DataType>String</DataType>
                <UpdateMode>LostFocus</UpdateMode>
                <IsHidden>false</IsHidden>
              </GridDataVisibleColumn>

SQL 查询看起来像:

执行 sp_xml_preparedocument @hdoc 输出,@blobXML

insert into @tmpblob
(
RunDate,
Token,
OwnerId,
MappingName ,
HeaderText  ,
WidthUnitType,
WidthValue
)
select 
    @RunDate,
    @Token,
    @OwnerId,
    MappingName,
    HeaderText,
    WidthUnitType,
    WidthValue
from OPENXML(@hdoc, '/GridDataTableProperties/VisibleColumns/GridDataVisibleColumn/MappingName', 2)
with(
    MappingName                     VARCHAR (64) './text()'  ,
    HeaderText                      VARCHAR (64) './text()'  ,
    WidthUnitType               VARCHAR (64) './UnitType/text()' ,
    WidthValue                          VARCHAR (64) './Value/text()' 
    )

EXEC sp_xml_removedocument @hdoc

假设@tmpblob 一切都是正确的。我也可以发布它,但它不会是 consie。我想知道如何以表格的形式显示这个 xml,例如

MappingName HeaderText WidthUnitType WidthValue
外部参照外部参照无 150

【问题讨论】:

    标签: sql sql-server xml sql-server-2008


    【解决方案1】:

    试试这个 -

    DECLARE @XML XML
    SELECT @XML = '
    <GridDataVisibleColumn>
        <HeaderText>XRef</HeaderText>
        <MappingName>XRef</MappingName>
        <MinimumWidth>0</MinimumWidth>
        <Width>
            <UnitType>None</UnitType>
            <Value>150</Value>
        </Width>
        <DataType>String</DataType>
    </GridDataVisibleColumn>'
    
    SELECT 
          MappingName = t.c.value('MappingName[1]', 'VARCHAR(64)')
        , HeaderText = t.c.value('HeaderText[1]', 'VARCHAR(64)') 
        , WidthUnitType = t.c.value('Width[1]/UnitType[1]', 'VARCHAR(64)')
        , WidthValue = t.c.value('Width[1]/Value[1]', 'VARCHAR(64)') 
    FROM @XML.nodes('/GridDataVisibleColumn') t(c)
    

    【讨论】:

      【解决方案2】:

      我想知道如何以表格的形式显示此 xml,例如

      您只需使用适合您的查询吗?

      这是适用于您在问题中发布的 XML 的修改版本。

      exec sp_xml_preparedocument @hdoc OUTPUT, @blobXML
      
      select 
        MappingName,
        HeaderText,
        WidthUnitType,
        WidthValue
      from openxml(@hdoc, '/GridDataVisibleColumn', 2)
      with(
          MappingName   VARCHAR (64),
          HeaderText    VARCHAR (64),
          WidthUnitType VARCHAR (64) 'Width/UnitType',
          WidthValue    VARCHAR (64) 'Width/Value' 
          )
      
      exec sp_xml_removedocument @hdoc
      

      【讨论】:

        猜你喜欢
        • 2016-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-08
        • 1970-01-01
        相关资源
        最近更新 更多