使用CTE插入解析的XML

时间:2014-05-14 11:26:25

标签: sql sql-server xml common-table-expression

我正在开发一个SQL Server 2012存储过程。

我想解析这个XML:

<Commissioning>
  <Item ProductionId="115100" ItemId="100" ItemPackagingLevelId="1" ItemFlag="0" TimeStamp="2014-04-28T16:02:59.913" Username="" Source="PLS" />
  <Item ProductionId="115101" ItemId="101" ItemPackagingLevelId="1" ItemFlag="0" TimeStamp="2014-04-28T16:03:00.113" Username="" Source="PLS" />
  <Item ProductionId="115102" ItemId="102" ItemPackagingLevelId="1" ItemFlag="0" TimeStamp="2014-04-28T16:03:00.317" Username="" Source="PLS" />
  <Item ProductionId="115103" ItemId="103" ItemPackagingLevelId="1" ItemFlag="0" TimeStamp="2014-04-28T16:03:00.517" Username="" Source="PLS" />
  <Item ProductionId="115104" ItemId="104" ItemPackagingLevelId="1" ItemFlag="0" TimeStamp="2014-04-28T16:03:00.727" Username="" Source="PLS" />
</Commissioning>

并在CTE中插入ItemIdItemPackagingLevelId,因为我想对该数据进行一些计算。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

喜欢这个......?

;with cte as (
    select 
        x.n.value('./@ItemId','varchar(100)') as ItemID,
        x.n.value('./@ItemPackagingLevelId','varchar(100)')  as LevelID
    from @x.nodes('/Commissioning/Item') x(n)
)
    select * from cte