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
Copy file name to clipboardExpand all lines: docs/t-sql/xml/replace-value-of-xml-dml.md
+41-40Lines changed: 41 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ Updates the value of a node in the document.
26
26
27
27
## Syntax
28
28
29
-
```sql
29
+
```syntaxsql
30
30
replace value of Expression1
31
31
with Expression2
32
32
```
@@ -47,7 +47,7 @@ The following examples of the **replace value of** XML DML statement illustrates
47
47
In the following example, a document instance is first assigned to a variable of **xml** type. Then, **replace value of** XML DML statements update values in the document.
48
48
49
49
```sql
50
-
DECLARE @myDoc xml;
50
+
DECLARE @myDoc XML;
51
51
SET @myDoc ='<Root>
52
52
<Location LocationID="10"
53
53
LaborHours="1.1"
@@ -61,13 +61,13 @@ SELECT @myDoc;
61
61
-- update text in the first manufacturing step
62
62
SET @myDoc.modify('
63
63
replace value of (/Root/Location/step[1]/text())[1]
64
-
with "new text describing the manu step"
64
+
with "new text describing the manu step"
65
65
');
66
66
SELECT @myDoc;
67
67
-- update attribute value
68
68
SET @myDoc.modify('
69
69
replace value of (/Root/Location/@LaborHours)[1]
70
-
with "100.0"
70
+
with "100.0"
71
71
');
72
72
SELECT @myDoc;
73
73
```
@@ -78,7 +78,7 @@ The target being updated must be, at most, one node that is explicitly specified
78
78
You can specify the **if** expression in Expression2 of the **replace value of XML DML** statement, as shown in the following example. Expression1 identifies the LaborHours attribute from the first work center is to be updated. Expression2 uses an **if** expression to determine the new value of the LaborHours attribute.
79
79
80
80
```sql
81
-
DECLARE @myDoc xml
81
+
DECLARE @myDoc XML
82
82
SET @myDoc ='<Root>
83
83
<Location LocationID="10"
84
84
LaborHours=".1"
@@ -105,10 +105,10 @@ SELECT @myDoc
105
105
The following example updates XML stored in a column:
@@ -137,52 +137,53 @@ This example replaces values in a manufacturing instructions document stored in
137
137
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.
138
138
139
139
```sql
140
-
use AdventureWorks
141
-
go
142
-
droptable T
143
-
go
144
-
createtableT(ProductModelID intprimary key,
145
-
Instructions xml (Production.ManuInstructionsSchemaCollection))
146
-
go
147
-
insert T
148
-
select ProductModelID, Instructions
149
-
fromProduction.ProductModel
150
-
where ProductModelID=7
151
-
go
140
+
USE AdventureWorks
141
+
GO
142
+
DROPTABLE T
143
+
GO
144
+
CREATETABLET(
145
+
ProductModelID INTPRIMARY KEY,
146
+
Instructions XML (Production.ManuInstructionsSchemaCollection))
replace value of (/MI:root/MI:Location/@LotSize)[1]
181
-
with 500 cast as xs:decimal ?
182
+
with 500 cast as xs:decimal ?
182
183
')
183
-
go
184
-
select Instructions
185
-
from T
184
+
GO
185
+
SELECT Instructions
186
+
FROM T
186
187
```
187
188
188
189
Note the use of **cast** when replacing LotSize value. It's required when the value must be of a specific type. In this example, if 500 were the value, explicit casting wouldn't be necessary.
0 commit comments