Question

I'm developing a SQL Server 2012 stored procedure.

I want to parse this 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>

And insert ItemId and ItemPackagingLevelId in a CTE because I want to do some calculations with that data.

How can I do it?

Was it helpful?

Solution

Like this...?

;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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top