Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit a639a35

Browse files
authored
added sql colorizer
and capitalized the keywords
1 parent c5f1f27 commit a639a35

1 file changed

Lines changed: 52 additions & 53 deletions

File tree

docs/t-sql/xml/insert-xml-dml.md

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ ms.author: genemi
2525

2626
## Syntax
2727

28-
```syntaxsql
29-
30-
insert
31-
Expression1 (
32-
{as first | as last} into | after | before
33-
Expression2
34-
)
28+
```syntaxsql
29+
insert Expression1 (
30+
{AS first | AS last} INTO | AFTER | BEFORE
31+
Expression2
32+
)
3533
```
3634

3735
[!INCLUDE[sql-server-tsql-previous-offline-documentation](../../includes/sql-server-tsql-previous-offline-documentation.md)]
@@ -57,10 +55,10 @@ insert
5755
### A. Inserting element nodes into the document
5856
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.
5957

60-
```
58+
```sql
6159
USE AdventureWorks;
6260
GO
63-
DECLARE @myDoc xml;
61+
DECLARE @myDoc XML;
6462
SET @myDoc = '<Root>
6563
<ProductDescription ProductID="1" ProductName="Road Bike">
6664
<Features>
@@ -70,30 +68,30 @@ SET @myDoc = '<Root>
7068
SELECT @myDoc;
7169
-- insert first feature child (no need to specify as first or as last)
7270
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]') ;
7573
SELECT @myDoc ;
7674
-- 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]
8179
') ;
8280
SELECT @myDoc ;
8381
-- insert third feature child. This one is the last child of <Features> so use 'as last'
8482
SELECT @myDoc
8583
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]
8987
')
9088
SELECT @myDoc ;
9189
-- Add fourth feature - this time as a sibling (and not a child)
9290
-- 'after' keyword is used (instead of as first or as last child)
9391
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]
9795
') ;
9896
SELECT @myDoc;
9997
GO
@@ -104,10 +102,10 @@ GO
104102
### B. Inserting multiple elements into the document
105103
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.
106104

107-
```
105+
```sql
108106
USE AdventureWorks;
109107
GO
110-
DECLARE @myDoc xml;
108+
DECLARE @myDoc XML;
111109
SET @myDoc = N'<Root>
112110
<ProductDescription ProductID="1" ProductName="Road Bike">
113111
<Features> </Features>
@@ -127,26 +125,26 @@ GO
127125
### C. Inserting attributes into a document
128126
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.
129127

130-
```
128+
```sql
131129
USE AdventureWorks;
132130
GO
133-
DECLARE @myDoc xml ;
131+
DECLARE @myDoc XML;
134132
SET @myDoc =
135133
'<Root>
136134
<Location LocationID="10" >
137135
<step>Manufacturing step 1 at this work center</step>
138136
<step>Manufacturing step 2 at this work center</step>
139137
</Location>
140138
</Root>' ;
141-
SELECT @myDoc ;
139+
SELECT @myDoc;
142140
-- insert LaborHours attribute
143141
SET @myDoc.modify('
144142
insert attribute LaborHours {".5" }
145-
into (/Root/Location[@LocationID=10])[1] ') ;
146-
SELECT @myDoc ;
143+
into (/Root/Location[@LocationID=10])[1] ');
144+
SELECT @myDoc;
147145
-- insert MachineHours attribute but its value is retrived from a sql variable @Hrs
148-
DECLARE @Hrs float ;
149-
SET @Hrs =.2 ;
146+
DECLARE @Hrs FLOAT;
147+
SET @Hrs =.2;
150148
SET @myDoc.modify('
151149
insert attribute MachineHours {sql:variable("@Hrs") }
152150
into (/Root/Location[@LocationID=10])[1] ');
@@ -158,18 +156,18 @@ insert (
158156
attribute SetupHours {".5" },
159157
attribute SomeOtherAtt {".2"}
160158
)
161-
into (/Root/Location[@LocationID=10])[1] ');
159+
INTO (/Root/Location[@LocationID=10])[1] ');
162160
SELECT @myDoc;
163161
GO
164162
```
165163

166164
### D. Inserting a comment node
167165
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.
168166

169-
```
167+
```sql
170168
USE AdventureWorks;
171169
GO
172-
DECLARE @myDoc xml;
170+
DECLARE @myDoc XML;
173171
SET @myDoc =
174172
'<Root>
175173
<Location LocationID="10" >
@@ -188,10 +186,10 @@ GO
188186
### E. Inserting a processing instruction
189187
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.
190188

191-
```
189+
```sql
192190
USE AdventureWorks;
193191
GO
194-
DECLARE @myDoc xml;
192+
DECLARE @myDoc XML;
195193
SET @myDoc =
196194
'<Root>
197195
<Location LocationID="10" >
@@ -210,10 +208,10 @@ GO
210208
### F. Inserting data using a CDATA section
211209
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 `&lt;`.
212210

213-
```
211+
```sql
214212
USE AdventureWorks;
215213
GO
216-
DECLARE @myDoc xml;
214+
DECLARE @myDoc XML;
217215
SET @myDoc =
218216
'<Root>
219217
<ProductDescription ProductID="1" ProductName="Road Bike">
@@ -241,10 +239,10 @@ GO
241239
### G. Inserting text node
242240
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.
243241

244-
```
242+
```sql
245243
USE AdventureWorks;
246244
GO
247-
DECLARE @myDoc xml;
245+
DECLARE @myDoc XML;
248246
SET @myDoc = '<Root>
249247
<ProductDescription ProductID="1" ProductName="Road Bike">
250248
<Features>
@@ -253,7 +251,7 @@ SET @myDoc = '<Root>
253251
</ProductDescription>
254252
</Root>'
255253
SELECT @myDoc;
256-
set @myDoc.modify('
254+
SET @myDoc.modify('
257255
insert text{"Product Catalog Description"}
258256
as first into (/Root)[1]
259257
');
@@ -263,11 +261,11 @@ SELECT @myDoc;
263261
### H. Inserting a new element into an untyped xml column
264262
The following example applies XML DML to update an XML instance stored in an **xml** type column:
265263

266-
```
264+
```sql
267265
USE AdventureWorks;
268266
GO
269-
CREATE TABLE T (i int, x xml);
270-
go
267+
CREATE TABLE T (i INT, x XML);
268+
GO
271269
INSERT INTO T VALUES(1,'<Root>
272270
<ProductDescription ProductID="1" ProductName="Road Bike">
273271
<Features>
@@ -276,7 +274,7 @@ INSERT INTO T VALUES(1,'<Root>
276274
</Features>
277275
</ProductDescription>
278276
</Root>');
279-
go
277+
GO
280278
-- insert a new element
281279
UPDATE T
282280
SET x.modify('insert <Material>Aluminium</Material> as first
@@ -287,7 +285,7 @@ GO
287285

288286
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.
289287

290-
```
288+
```sql
291289
-- check the update
292290
SELECT x.query(' //ProductDescription/Features')
293291
FROM T;
@@ -297,10 +295,10 @@ GO
297295
### I. Inserting based on an if condition statement
298296
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.
299297

300-
```
298+
```sql
301299
USE AdventureWorks;
302300
GO
303-
DECLARE @myDoc xml;
301+
DECLARE @myDoc XML;
304302
SET @myDoc =
305303
'<Root>
306304
<Location LocationID="10" LaborHours="1.2" >
@@ -321,10 +319,10 @@ GO
321319

322320
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.
323321

324-
```
322+
```sql
325323
USE AdventureWorks;
326324
GO
327-
DECLARE @myDoc xml;
325+
DECLARE @myDoc XML;
328326
SET @myDoc =
329327
'<Root>
330328
<Location LocationID="10" LaborHours="1.2" >
@@ -359,13 +357,14 @@ GO
359357

360358
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.
361359

362-
```
360+
```sql
363361
USE AdventureWorks;
364362
GO
365363
DROP TABLE T;
366364
GO
367-
CREATE TABLE T(ProductModelID int primary key,
368-
Instructions xml (Production.ManuInstructionsSchemaCollection));
365+
CREATE TABLE T(
366+
ProductModelID INT PRIMARY KEY,
367+
Instructions XML (Production.ManuInstructionsSchemaCollection));
369368
GO
370369
INSERT T
371370
SELECT ProductModelID, Instructions
@@ -378,7 +377,7 @@ FROM T;
378377
--1) insert a new manu. Location. The <Root> specified as
379378
-- expression 2 in the insert() must be singleton.
380379
UPDATE T
381-
set Instructions.modify('
380+
SET Instructions.modify('
382381
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
383382
insert <MI:Location LocationID="1000" >
384383
<MI:step>New instructions go here</MI:step>

0 commit comments

Comments
 (0)