-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102_openjson()_in_detail.sql
98 lines (82 loc) · 2.01 KB
/
102_openjson()_in_detail.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
90
91
92
93
94
95
96
97
98
declare @json_text nvarchar(max);
set @json_text = '
{
"websiteURL": "www.test.edu",
"email": "[email protected]",
"phone": 123456798,
"address": {
"address1": "1 Oak Grove",
"address2": "London",
"address3": "UK"
},
"accreditations": [
{
"name": "Indicator1",
"value": "True"
},
{
"name": "Indicator2",
"value": "False"
},
{
"name": "Indicator3",
"value": "False"
}
]
}
'
--select @json_text;
--select * from openjson(@json_text)
--DEFINING SCHEMA EXPLICITLY
/*select * from openjson(@json_text)
with ( websiteURL varchar(100),
email varchar(100),
phone int,
address nvarchar(max) '$.address' as json,
accreditations nvarchar(max) '$.accreditations' as json
)
--CROSS APPLY WITH OPENJSON()
select * from openjson(@json_text)
with ( websiteURL varchar(100),
email varchar(100),
phone int,
address nvarchar(max) '$.address' as json,
accreditations nvarchar(max) '$.accreditations' as json
)cross apply openjson(address) with
( address1 varchar(100),
address2 varchar(100),
address3 varchar(100)
)
cross apply openjson(accreditations) with
( name nvarchar(max) '$' as json)
cross apply openjson(name) with
( name varchar(100),
value varchar(100)
)
*/
--CROSS APPLY AND SELECTING ONLY REQUIRED COLUMNS
select websiteURL,
email,
phone,
address1,
address2,
address3,
name,
value
from openjson(@json_text)
with ( websiteURL varchar(100),
email varchar(100),
phone int,
address nvarchar(max) '$.address' as json,
accreditations nvarchar(max) '$.accreditations' as json
)cross apply openjson(address) with
( address1 varchar(100),
address2 varchar(100),
address3 varchar(100)
)
cross apply openjson(accreditations) with
( accreditation nvarchar(max) '$' as json)
cross apply openjson(accreditation) with
( name varchar(100),
value varchar(100)
)