-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_40_crossApplyXml.sql
89 lines (74 loc) · 2.09 KB
/
08_40_crossApplyXml.sql
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
select *
from [Warehouse].[Colors]
for xml auto;
select *
from [Warehouse].[Colors]
for xml raw;
select *
from [Warehouse].[Colors]
for json auto;
declare @xml XML
SET @xml = (SELECT ColorId
FROm Warehouse.Colors
FOR XML AUTO);
select @xml;
SET @xml = (SELECT ColorId AS Id
FROm Warehouse.Colors AS C
FOR XML AUTO);
select @xml;
SELECT t.C.value('@Id','INT')
FROM @xml.nodes('C') as t(C);
declare @xml XML
SET @xml =
(SELECT top 100
Invoices.InvoiceID,
Invoices.InvoiceDate,
Invoices.SalespersonPersonID,
InvoiceLines.InvoiceLineID,
InvoiceLines.Quantity,
InvoiceLines.UnitPrice,
InvoiceLines.TaxAmount
FROm Sales.Invoices
JOIN Sales.InvoiceLines
ON Invoices.InvoiceID = InvoiceLines.InvoiceID
FOR XML AUTO, Elements, root('invoices'));
select @xml;
select Ids.InvoiceId,
InvoiceDate,
LineId
from @xml.nodes('./invoices') AS invoicesDoc(Invoices)
cross apply
Invoices.nodes('(./Sales.Invoices)') SI(InvoiceEx)
cross apply
(select InvoiceEx.value('(./InvoiceID[1])', 'INT') AS InvoiceId) AS Ids
cross apply
(select InvoiceEx.value('(./InvoiceDate[1])', 'DATE') AS InvoiceDate) AS Dates
cross apply
InvoiceEx.nodes('(./Sales.InvoiceLines)') AS SIL(T)
cross apply
(select T.value('(./InvoiceLineID[1])', 'INT') AS LineId) AS lines;
with xmlnamespaces
(
'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' as ns
)
select JobCandidateID
,Name
,Education=stuff(EduList,1,2,''),
Resume
from HumanResources.JobCandidate
cross apply
Resume.nodes('/ns:Resume') F_ResumeNode(ResumeNode)
cross apply
ResumeNode.nodes('(./ns:Name)') F_NameNode(NameNode)
cross apply
(select Name=NameNode.value('(./ns:Name.First[1])','nvarchar(50)')
+' '
+NameNode.value('(./ns:Name.Last[1])','nvarchar(50)')
) F_Name
cross apply
(select EduList=ResumeNode.query('for $p in (./ns:Education)
order by $p/ns:Edu.EndDate
return concat("; ",string($p/ns:Edu.School))'
).value('.','nvarchar(200)')
) F_Edu
where JobCandidateID<=10;