I have an XML block that I am sending to my stored procedure.
<vehicles>
<licensePlate>ABC123</licensePlate>
<vehicle>
<model>Ford</model>
<color>Blue</color>
<carPool>
<employee>
<empID>111</empID>
</employee>
<employee>
<empID>222</empID>
</employee>
<employee>
<empID>333</empID>
</employee>
</carPool>
</vehicle>
</vehicles>
I then use a select statement to parse out the data that I need from this XML block.
INSERT INTO licensePlates (carColor, carModel, licensePlate, empID, dateAdded)
SELECT ParamValues.x2.value('color[1]', 'VARCHAR(100)'),
ParamValues.x2.value('model[1]', 'VARCHAR(100)'),
ParamValues.x2.value('../licensePlate[1]', 'VARCHAR(100)'),
@empID,
GETDATE()
FROM @xmlData.nodes('/vehicles/vehicle') AS ParamValues(x2)
I need to store the XML contained within the tag <carPool>
into a column in this table.
So I'm getting this XML block, and need a piece of that to not be parsed and just go directly to the table:
<carPool>
<employee>
<empID>111</empID>
</employee>
<employee>
<empID>222</empID>
</employee>
<employee>
<empID>333</empID>
</employee>
</carPool>
How can I go about doing this?
This is an example of what the inserted record would look like.
Assuming your stored procedure has a parameter called @Input XML
, you could use this code:
INSERT INTO dbo.YourTable(XmlColumn)
SELECT @input.query('/vehicles/vehicle/carPool')
That should select the <carPool>
XML tag and insert it into the XML column of your table.
I think this is what you are looking for:
SELECT ParamValues.x2.value('color[1]', 'VARCHAR(100)'),
ParamValues.x2.value('model[1]', 'VARCHAR(100)'),
ParamValues.x2.value('../licensePlate[1]', 'VARCHAR(100)'),
@empID,
GETDATE()
, carPoolMembers = x2.query('./carPool')
FROM @xmlData.nodes('/vehicles/vehicle') AS ParamValues(x2)
You can insert the node directly
INSERT INTO licensePlates (carColor, carModel, licencePlate, empId, dateAdded, carPoolMembers)
SELECT ParamValues.x2.value('color[1]', 'VARCHAR(100)'),
ParamValues.x2.value('model[1]', 'VARCHAR(100)'),
ParamValues.x2.value('../licensePlate[1]', 'VARCHAR(100)'),
@empID,
GETDATE(),
ParamValues.x2.query('./carPool')
FROM @xmlData.nodes('/vehicles/vehicle') AS ParamValues(x2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.