Andrew Berry
Andrew Berry

Reputation: 863

Insert XML Node into a SQL column in a table

I have an XML file that I am passing into a stored procedure.

I also have a table. The table has the columns VehicleReg | XML | ProcessedDate

My XML comes in like so:

<vehicles> 
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
</vehicles>

What I need to do is read the xml and insert the vehiclereg and the full vehicle xml string into each row (the dateprocessed is a getdate() so not a problem).

I was working on something like below but had no luck:

DECLARE @XmlData XML
Set @XmlData = EXAMPLE XML
SELECT T.Vehicle.value('(vehiclereg)[1]', 'NVARCHAR(10)') AS vehiclereg,
       T.Vehicle.value('.', 'NVARCHAR(MAX)'),
       GETDATE()
FROM @XmlData.nodes('Vehicles/Vehicle') AS T(Vehicle)

I was wondering if someone could point me in the right direction?

Regards

Upvotes: 4

Views: 3796

Answers (3)

TT.
TT.

Reputation: 16137

You can get the XML by XML.query(), in this case query('.') on the vehicles/vehicle node to get the inner XML.

DECLARE @x XML=
'<vehicles> 
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
  <vehicle>
    <vehiclereg>AB13QQT</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
</vehicles>';

DECLARE @t TABLE(VehicleReg VARCHAR(128),[XML] XML,ProcessedDate DATETIME);

INSERT INTO @t(VehicleReg,[XML],ProcessedDate)
SELECT VehicleReg=n.v.value('(vehiclereg)[1]','VARCHAR(128)'),
       [XML]=n.v.query('.'),
       ProcessedDate=GETDATE()
FROM @x.nodes('vehicles/vehicle') AS n(v);

SELECT * FROM @t;

Result:

+------------+------------------------------------------------------------------------------------+-------------------------+
| VehicleReg |                                XML                                                 |      ProcessedDate      |
+------------+------------------------------------------------------------------------------------+-------------------------+
| AB12CBE    | <vehicle><vehiclereg>AB12CBE</vehiclereg><anotherprop>BLAH</anotherprop></vehicle> | 2016-03-01 15:21:37.640 |
| AB13QQT    | <vehicle><vehiclereg>AB13QQT</vehiclereg><anotherprop>BLAH</anotherprop></vehicle> | 2016-03-01 15:21:37.640 |
+------------+------------------------------------------------------------------------------------+-------------------------+

Upvotes: 2

Fuzzy
Fuzzy

Reputation: 3810

Just need to remember XML is case sensitive. You had:

FROM @XmlData.nodes('Vehicles/Vehicle') AS T(Vehicle)

but you should have had:

FROM @XmlData.nodes('/vehicles/vehicle') AS T(Vehicle)

Also as TT pointed out there was no column named Registration

This should do it:

DECLARE @XmlData XML
Set @XmlData = '<vehicles> 
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
</vehicles>'
SELECT  Vehicle.value('(vehiclereg)[1]', 'NVARCHAR(10)') AS vehiclereg,
        Vehicle.value('.', 'NVARCHAR(MAX)'),
       GETDATE()
FROM @XmlData.nodes('/vehicles/vehicle') AS T(Vehicle)

Result:

enter image description here

This would return XML:

DECLARE @XmlData XML
Set @XmlData = '<vehicles> 
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
</vehicles>'
SELECT T.Vehicle.value('(vehiclereg)[1]', 'NVARCHAR(10)') AS vehiclereg,
       T.Vehicle.query('.'),
       GETDATE()
FROM @XmlData.nodes('vehicles/vehicle') AS T(Vehicle)

Result:

enter image description here

Upvotes: 4

Ruslan K.
Ruslan K.

Reputation: 1981

Full query as you want:

DECLARE @XmlData XML
Set @XmlData = '<vehicles> 
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
  <vehicle>
    <vehiclereg>AB12CBE</vehiclereg>
    <anotherprop>BLAH</anotherprop>
  </vehicle>
</vehicles>'
SELECT T.Vehicle.value('./vehiclereg[1]', 'NVARCHAR(10)') AS vehiclereg,
       T.Vehicle.query('.'),
       GETDATE()
FROM @XmlData.nodes('/vehicles/vehicle') AS T(Vehicle)

enter image description here

Upvotes: 4

Related Questions