-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSQL-XML_Manipulation.sql
More file actions
51 lines (40 loc) · 1.68 KB
/
TSQL-XML_Manipulation.sql
File metadata and controls
51 lines (40 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
-- This is a collection of SQL Scraps useful in manipulating XML in a SQL Stored proc or script
--== get rid of NULL values, since the .modify() doesn't know how to handle NULL
UPDATE TableName
SET XMLParam = ''
WHERE XMLParam IS NULL
--== Insert the parameter "paramname" if it does not already exist. Default value is "0"
UPDATE TableName
SET XMLParam.modify('
insert
into (/paramlist)[1]')
WHERE XMLParam.exist('(/paramlist/param[@name="paramname"])[1]')=0
--== Modify the parameter "paramname"
UPDATE TableName
SET XMLParam.modify('
replace value of (/paramlist/param[@name="paramname"]/@value)[1]
with "1"
')
--== How to pull out the item name and item value from XML
DECLARE @iXML XML
SELECT @iXML = '<root><ID>1</ID><ThingNumber>2</ThingNumber></root>'
SELECT c.query('local-name(.)').value('.','varchar(50)') as 'ElementName',
c.value('.', 'varchar(200)') as 'ElementValue'
FROM @iXML.nodes('root/node()') t(c)
--== Get an element value from a node with a particular other element value
declare @xml xml = '<root>
<Properties><id>12345</id><name>Language</name><value>English</value></Properties>
<Properties><id>67890</id><name>Node1</name><value>Test</value></Properties>
</root>'
-- Get Properties node's value element text when name element is a specific value
select c.value('(Properties[./name/text() = "Language"]/value)[1]','varchar(255)') as Language
,c.value('(Properties[./name/text() = "Node1"]/value)[1]','varchar(255)') as CategoryType
from @xml.nodes('/root') t(c)
--Insert a new attribute
declare @test xml
set @test = '
'
set @test.modify('
insert attribute MiddleName {"Smith" }
into (/root/Employee[@EmployeeID=6700])[1] ')
select @test