You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following example illustrates how to insert elements into a document. First, an XML document is assigned to a variable of **xml** type. Then, through several **insert** XML DML statements, the example illustrates how element nodes are inserted in the document. After each insert, the SELECT statement displays the result.
-- insert first feature child (no need to specify as first or as last)
72
70
SET @myDoc.modify('
73
-
insert <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
74
-
into (/Root/ProductDescription/Features)[1]') ;
71
+
INSERT <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
72
+
INTO (/Root/ProductDescription/Features)[1]') ;
75
73
SELECT @myDoc ;
76
74
-- insert second feature. We want this to be the first in sequence so use 'as first'
77
-
set @myDoc.modify('
78
-
insert <Warranty>1 year parts and labor</Warranty>
79
-
as first
80
-
into (/Root/ProductDescription/Features)[1]
75
+
SET @myDoc.modify('
76
+
INSERT <Warranty>1 year parts and labor</Warranty>
77
+
AS first
78
+
INTO (/Root/ProductDescription/Features)[1]
81
79
') ;
82
80
SELECT @myDoc ;
83
81
-- insert third feature child. This one is the last child of <Features> so use 'as last'
84
82
SELECT @myDoc
85
83
SET @myDoc.modify('
86
-
insert <Material>Aluminium</Material>
87
-
as last
88
-
into (/Root/ProductDescription/Features)[1]
84
+
INSERT <Material>Aluminium</Material>
85
+
AS last
86
+
INTO (/Root/ProductDescription/Features)[1]
89
87
')
90
88
SELECT @myDoc ;
91
89
-- Add fourth feature - this time as a sibling (and not a child)
92
90
-- 'after' keyword is used (instead of as first or as last child)
93
91
SELECT @myDoc ;
94
-
set @myDoc.modify('
95
-
insert <BikeFrame>Strong long lasting</BikeFrame>
96
-
after (/Root/ProductDescription/Features/Material)[1]
92
+
SET @myDoc.modify('
93
+
INSERT <BikeFrame>Strong long lasting</BikeFrame>
94
+
AFTER (/Root/ProductDescription/Features/Material)[1]
97
95
') ;
98
96
SELECT @myDoc;
99
97
GO
@@ -104,10 +102,10 @@ GO
104
102
### B. Inserting multiple elements into the document
105
103
In the following example, a document is first assigned to a variable of **xml** type. Then, a sequence of two elements, representing product features, is assigned to a second variable of **xml** type. This sequence is then inserted into the first variable.
The following example illustrates how attributes are inserted in a document. First, a document is assigned to an **xml** type variable. Then, a series of **insert** XML DML statements is used to insert attributes into the document. After each attribute insertion, the SELECT statement displays the result.
129
127
130
-
```
128
+
```sql
131
129
USE AdventureWorks;
132
130
GO
133
-
DECLARE @myDoc xml ;
131
+
DECLARE @myDoc XML;
134
132
SET @myDoc =
135
133
'<Root>
136
134
<Location LocationID="10" >
137
135
<step>Manufacturing step 1 at this work center</step>
138
136
<step>Manufacturing step 2 at this work center</step>
139
137
</Location>
140
138
</Root>' ;
141
-
SELECT @myDoc;
139
+
SELECT @myDoc;
142
140
-- insert LaborHours attribute
143
141
SET @myDoc.modify('
144
142
insert attribute LaborHours {".5" }
145
-
into (/Root/Location[@LocationID=10])[1] ');
146
-
SELECT @myDoc;
143
+
into (/Root/Location[@LocationID=10])[1] ');
144
+
SELECT @myDoc;
147
145
-- insert MachineHours attribute but its value is retrived from a sql variable @Hrs
In this query, an XML document is first assigned to a variable of **xml** type. Then, XML DML is used to insert a comment node after the first <`step`> element.
168
166
169
-
```
167
+
```sql
170
168
USE AdventureWorks;
171
169
GO
172
-
DECLARE @myDoc xml;
170
+
DECLARE @myDoc XML;
173
171
SET @myDoc =
174
172
'<Root>
175
173
<Location LocationID="10" >
@@ -188,10 +186,10 @@ GO
188
186
### E. Inserting a processing instruction
189
187
In the following query, an XML document is first assigned to a variable of **xml** type. Then, the XML DML keyword is used to insert a processing instruction at the start of the document.
190
188
191
-
```
189
+
```sql
192
190
USE AdventureWorks;
193
191
GO
194
-
DECLARE @myDoc xml;
192
+
DECLARE @myDoc XML;
195
193
SET @myDoc =
196
194
'<Root>
197
195
<Location LocationID="10" >
@@ -210,10 +208,10 @@ GO
210
208
### F. Inserting data using a CDATA section
211
209
When you insert text that includes characters that are not valid in XML, such as < or >, you can use CDATA sections to insert the data as shown in the following query. The query specifies a CDATA section, but it is added as a text node with any invalid characters converted to entities. For example, `<` is saved as `<`.
In this query, an XML document is first assigned to a variable of **xml** type. Then, XML DML is used to insert a text node as the first child of the <`Root`> element. The text constructor is used to specify the text.
@@ -276,7 +274,7 @@ INSERT INTO T VALUES(1,'<Root>
276
274
</Features>
277
275
</ProductDescription>
278
276
</Root>');
279
-
go
277
+
GO
280
278
-- insert a new element
281
279
UPDATE T
282
280
SETx.modify('insert <Material>Aluminium</Material> as first
@@ -287,7 +285,7 @@ GO
287
285
288
286
Again, when the <`Material`> element node is inserted, the path expression must return a single target. This is explicitly specified by adding a [1] at the end of the expression.
289
287
290
-
```
288
+
```sql
291
289
-- check the update
292
290
SELECTx.query(' //ProductDescription/Features')
293
291
FROM T;
@@ -297,10 +295,10 @@ GO
297
295
### I. Inserting based on an if condition statement
298
296
In the following example, an IF condition is specified as part of Expression1 in the **insert** XML DML statement. If the condition is True, an attribute is added to the <`WorkCenter`> element.
299
297
300
-
```
298
+
```sql
301
299
USE AdventureWorks;
302
300
GO
303
-
DECLARE @myDoc xml;
301
+
DECLARE @myDoc XML;
304
302
SET @myDoc =
305
303
'<Root>
306
304
<Location LocationID="10" LaborHours="1.2" >
@@ -321,10 +319,10 @@ GO
321
319
322
320
The following example is similar, except that the **insert** XML DML statement inserts an element in the document if the condition is True. That is, if the <`WorkCenter`> element has less than or is equal to two <`step`> child elements.
323
321
324
-
```
322
+
```sql
325
323
USE AdventureWorks;
326
324
GO
327
-
DECLARE @myDoc xml;
325
+
DECLARE @myDoc XML;
328
326
SET @myDoc =
329
327
'<Root>
330
328
<Location LocationID="10" LaborHours="1.2" >
@@ -359,13 +357,14 @@ GO
359
357
360
358
In the example, you first create a table (T) with a typed **xml** column, in the AdventureWorks database. You then copy a manufacturing instructions XML instance from the Instructions column in the ProductModel table into table T. Insertions are then applied to XML in table T.
361
359
362
-
```
360
+
```sql
363
361
USE AdventureWorks;
364
362
GO
365
363
DROPTABLE T;
366
364
GO
367
-
CREATE TABLE T(ProductModelID int primary key,
368
-
Instructions xml (Production.ManuInstructionsSchemaCollection));
365
+
CREATETABLET(
366
+
ProductModelID INTPRIMARY KEY,
367
+
Instructions XML (Production.ManuInstructionsSchemaCollection));
369
368
GO
370
369
INSERT T
371
370
SELECT ProductModelID, Instructions
@@ -378,7 +377,7 @@ FROM T;
378
377
--1) insert a new manu. Location. The <Root> specified as
379
378
-- expression 2 in the insert() must be singleton.
0 commit comments