[英]Insert XML Node into a SQL column in a table
I have an XML file that I am passing into a stored procedure. 我有一个XML文件,我将其传递给存储过程。
I also have a table. 我也有一张桌子。 The table has the columns VehicleReg |
该表有VehicleReg |列 XML |
XML | ProcessedDate
ProcessedDate
My XML comes in like so: 我的XML就是这样的:
<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). 我需要做的是读取xml并将vehiclereg和整车xml字符串插入每一行(dateprocessed是getdate()所以不是问题)。
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 问候
Just need to remember XML is case sensitive. 只需要记住XML区分大小写。 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
此外,TT指出没有名为
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: 结果:
This would return XML: 这将返回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: 结果:
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)
You can get the XML by XML.query()
, in this case query('.')
on the vehicles/vehicle
node to get the inner XML. 您可以通过
XML.query()
获取XML,在这种情况下,在vehicles/vehicle
节点上query('.')
以获取内部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 |
+------------+------------------------------------------------------------------------------------+-------------------------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.