Skip to content

Instantly share code, notes, and snippets.

@thanley11
Created September 19, 2018 18:13
Show Gist options
  • Save thanley11/427c0856ea3faecca63e8edd65abdc23 to your computer and use it in GitHub Desktop.
Save thanley11/427c0856ea3faecca63e8edd65abdc23 to your computer and use it in GitHub Desktop.
Handling XML in SQL
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