Created
September 19, 2018 18:13
-
-
Save thanley11/427c0856ea3faecca63e8edd65abdc23 to your computer and use it in GitHub Desktop.
Handling XML in SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @myDoc xml ; | |
SET @myDoc = | |
'<Root> | |
<Location LocationID="10" > | |
<step>Manufacturing step 1 at this work center</step> | |
<step>Manufacturing step 2 at this work center</step> | |
</Location> | |
</Root>' ; | |
SELECT @myDoc ; | |
-- insert LaborHours attribute | |
SET @myDoc.modify(' | |
insert attribute LaborHours {".5" } | |
into (/Root/Location[@LocationID=10])[1] ') ; | |
SELECT @myDoc ; | |
-- insert MachineHours attribute but its value is retrived from a sql variable @Hrs | |
DECLARE @Hrs float ; | |
SET @Hrs =.2 ; | |
SET @myDoc.modify(' | |
insert attribute MachineHours {sql:variable("@Hrs") } | |
into (/Root/Location[@LocationID=10])[1] '); | |
SELECT @myDoc; | |
-- insert sequence of attribute nodes (note the use of ',' and () | |
-- around the attributes. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment