Skip to content

Commit aeca8bd

Browse files
committed
Import from Azure Blob + several minor changes
1. Added two additional scripts in Belgrade Product catalog demo 2. Added Channel9 JSON demo script. 3. Added BULK IMPORT from Blob Storage sample
1 parent 5f8a8d7 commit aeca8bd

File tree

11 files changed

+424
-0
lines changed

11 files changed

+424
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
DROP TABLE IF EXISTS xtp.Product;
2+
GO
3+
DROP SCHEMA IF EXISTS xtp;
4+
GO
5+
6+
CREATE SCHEMA xtp;
7+
GO
8+
9+
CREATE TABLE xtp.Product (
10+
ProductID int IDENTITY PRIMARY KEY NONCLUSTERED,
11+
Name nvarchar(50) NOT NULL,
12+
Color nvarchar(15) NULL,
13+
Size nvarchar(5) NULL,
14+
Price money NOT NULL,
15+
Quantity int NULL,
16+
CompanyID int,
17+
Data nvarchar(4000),
18+
Tags nvarchar(4000),
19+
DateModified datetime2(0) NOT NULL DEFAULT (GETUTCDATE())
20+
) WITH (MEMORY_OPTIMIZED=ON)
21+
GO
22+
23+
DECLARE @products NVARCHAR(MAX) =
24+
N'[{"ProductID":15,"Name":"Adjustable Race","Color":"Magenta","Size":"62","Price":100.0000,"Quantity":75,"CompanyID":1,"Data":{"Type":"Part","MadeIn":"China"},"DateModified":"2016-02-11T21:27:32"},{"ProductID":16,"Name":"Bearing Ball","Color":"Magenta","Size":"62","Price":15.9900,"Quantity":90,"CompanyID":2,"Data":{"ManufacturingCost":11.672700,"Type":"Part","MadeIn":"China"},"Tags":["promo"],"DateModified":"2016-02-11T21:27:32"},{"ProductID":17,"Name":"BB Ball Bearing","Color":"Magenta","Size":"62","Price":28.9900,"Quantity":80,"CompanyID":3,"Data":{"ManufacturingCost":21.162700,"Type":"Part","MadeIn":"China"},"DateModified":"2016-02-11T21:27:32"},{"ProductID":18,"Name":"Blade","Color":"Magenta","Size":"62","Price":18.0000,"Quantity":45,"CompanyID":4,"Data":{},"Tags":["new"],"DateModified":"2016-02-11T21:27:32"},{"ProductID":19,"Name":"Sport-100 Helmet, Red","Color":"Red","Size":"72","Price":41.9900,"Quantity":38,"CompanyID":3,"Data":{"ManufacturingCost":30.652700,"Type":"Еquipment","MadeIn":"China"},"Tags":["promo"],"DateModified":"2016-02-11T21:27:32"},{"ProductID":20,"Name":"Sport-100 Helmet, Black","Color":"Black","Size":"72","Price":31.4900,"Quantity":60,"CompanyID":1,"Data":{"ManufacturingCost":22.987700,"Type":"Еquipment","MadeIn":"China"},"Tags":["new","promo"],"DateModified":"2016-02-11T21:27:32"},{"ProductID":21,"Name":"Mountain Bike Socks, M","Color":"White","Size":"M","Price":560.9900,"Quantity":30,"CompanyID":2,"Data":{"Type":"Clothes"},"Tags":["sales","promo"],"DateModified":"2016-02-11T21:27:32"},{"ProductID":22,"Name":"Mountain Bike Socks, L","Color":"White","Size":"L","Price":120.9900,"Quantity":20,"CompanyID":3,"Data":{"ManufacturingCost":88.322700,"Type":"Clothes"},"Tags":["sales","promo"],"DateModified":"2016-02-11T21:27:32"},{"ProductID":23,"Name":"Long-Sleeve Logo Jersey, XL","Color":"Multi","Size":"XL","Price":44.9900,"Quantity":60,"CompanyID":4,"Data":{"ManufacturingCost":32.842700,"Type":"Clothes"},"Tags":["sales","promo"],"DateModified":"2016-02-11T21:27:32"},{"ProductID":24,"Name":"Road-650 Black, 52","Color":"Black","Size":"52","Price":704.6900,"Quantity":70,"CompanyID":5,"Data":{"Type":"Bike","MadeIn":"UK"},"DateModified":"2016-02-11T21:27:32"},{"ProductID":25,"Name":"Mountain-100 Silver, 38","Color":"Silver","Size":"38","Price":359.9900,"Quantity":45,"CompanyID":1,"Data":{"ManufacturingCost":262.792700,"Type":"Bike","MadeIn":"UK"},"Tags":["promo"],"DateModified":"2016-02-11T21:27:32"},{"ProductID":26,"Name":"Road-250 Black, 48","Color":"Black","Size":"48","Price":299.0200,"Quantity":25,"CompanyID":2,"Data":{"ManufacturingCost":218.284600,"Type":"Bike","MadeIn":"UK"},"Tags":["new","promo"],"DateModified":"2016-02-11T21:27:32"},{"ProductID":27,"Name":"ML Bottom Bracket","Price":101.2400,"Quantity":50,"CompanyID":3,"Data":{"Type":"Part","MadeIn":"China"},"DateModified":"2016-02-11T21:27:32"},{"ProductID":28,"Name":"HL Bottom Bracket","Price":121.4900,"Quantity":65,"CompanyID":4,"Data":{"ManufacturingCost":88.687700,"Type":"Part","MadeIn":"China"},"DateModified":"2016-02-11T21:27:32"}]'
25+
INSERT INTO xtp.Product (ProductID, Name, Color, Size, Price, Quantity, CompanyID, Data, Tags, DateModified)
26+
SELECT ProductID, Name, Color, Size, Price, Quantity, CompanyID, Data, Tags, DateModified
27+
FROM OPENJSON (@products) WITH(
28+
ProductID int,Name nvarchar(50),Color nvarchar(15),Size nvarchar(5),Price money,Quantity int,CompanyID int,
29+
Data nvarchar(MAX) AS JSON,Tags nvarchar(MAX) AS JSON,
30+
DateModified datetime2(0)
31+
)
32+
GO
33+
34+
DROP PROCEDURE IF EXISTS xtp.InsertProductFromJson
35+
GO
36+
CREATE PROCEDURE xtp.InsertProductFromJson(@ProductJson NVARCHAR(MAX))
37+
WITH SCHEMABINDING, NATIVE_COMPILATION
38+
AS BEGIN
39+
ATOMIC WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english')
40+
INSERT INTO xtp.Product(Name,Color,Size,Price,Quantity,CompanyID,Data,Tags)
41+
OUTPUT INSERTED.ProductID
42+
SELECT Name,Color,Size,Price,Quantity,CompanyID,Data,Tags
43+
FROM OPENJSON(@ProductJson)
44+
WITH ( Name nvarchar(100) N'strict $."Name"',
45+
Color nvarchar(30),
46+
Size nvarchar(10),
47+
Price money N'strict $."Price"',
48+
Quantity int,
49+
CompanyID int,
50+
Data nvarchar(max) AS JSON,
51+
Tags nvarchar(max) AS JSON) as json
52+
END
53+
GO
54+
55+
DROP PROCEDURE IF EXISTS xtp.UpdateProductFromJson
56+
GO
57+
CREATE PROCEDURE xtp.UpdateProductFromJson(@ProductID int, @ProductJson NVARCHAR(MAX))
58+
AS BEGIN
59+
60+
UPDATE xtp.Product SET
61+
Name = json.Name,
62+
Color = json.Color,
63+
Size = json.Size,
64+
Price = json.Price,
65+
Quantity = json.Quantity,
66+
Data = ISNULL(json.Data, dbo.Product.Data),
67+
Tags = ISNULL(json.Tags,dbo.Product.Tags)
68+
FROM OPENJSON(@ProductJson)
69+
WITH ( Name nvarchar(100) N'strict $."Name"',
70+
Color nvarchar(30),
71+
Size nvarchar(10),
72+
Price money N'strict $."Price"',
73+
Quantity int,
74+
Data nvarchar(max) AS JSON,
75+
Tags nvarchar(max) AS JSON) as json
76+
WHERE xtp.Product.ProductID = @ProductID
77+
78+
END
79+
GO
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
DROP VIEW IF EXISTS History.Product
2+
GO
3+
DROP SCHEMA IF EXISTS History
4+
GO
5+
CREATE SCHEMA History
6+
GO
7+
8+
CREATE VIEW History.Product
9+
AS SELECT * FROM ProductCatalog.History.Product
10+
GO
11+
12+
drop function if exists dbo.diff_Product
13+
go
14+
create function dbo.diff_Product (@id int, @date datetime2(0))
15+
returns table
16+
as
17+
return (
18+
select * from ProductCatalog.dbo.diff_Product (@id, @date)
19+
)
20+
GO
21+
22+
23+
drop procedure if exists dbo.GetProducts
24+
go
25+
create procedure dbo.GetProducts as
26+
begin
27+
begin tran
28+
EXEC ProductCatalog.dbo.GetProducts
29+
commit
30+
end
31+
GO
32+
33+
drop procedure if exists dbo.GetProductsAsOf
34+
go
35+
create procedure dbo.GetProductsAsOf (@date datetime2) as
36+
begin
37+
begin tran
38+
EXEC ProductCatalog.dbo.GetProductsAsOf @date
39+
commit
40+
end
41+
GO
42+
43+
GO
44+
drop procedure if exists dbo.RestoreProduct
45+
GO
46+
create procedure dbo.RestoreProduct (@productid int, @date datetime2) as
47+
begin
48+
begin tran
49+
EXEC ProductCatalog.dbo.RestoreProduct @productid, @date
50+
commit
51+
end
52+
GO
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
declare @json nvarchar(max) =
2+
N'{
3+
"Name": "Vlade",
4+
"Surname": "Divac",
5+
"Born": { "DoB":"1968-03-02","Town":"Prijepolje", "Country": "Serbia"},
6+
"NBA Stat": { "pts":13398, "ppg": 11.8, "rebounds": 9326, "rpg": 8.2, "blocks": 1631, "bpg": 1.4},
7+
"Teams": ["Los Angeles Lakers","Sacramento Kings","Partizan"],
8+
"Career": [
9+
{"team":"Sloga", "period":{"start":1983, "end":1986}},
10+
{"team":"Partizan", "period":{"start":1986, "end":1989}},
11+
{"team":"Los Angeles Lakers","gp":540, "gs":450, "period":{"start":1989, "end":1996}},
12+
{"team":"Charlotte Hornets","gp":145, "gs":121,"period":{"start":1996, "end":1998}},
13+
{"team":"Sacramento Kings","gp":454, "gs":453,"period":{"start":1998, "end":2004}},
14+
{"team":"Los Angeles Lakers", "gp":15, "gs":0,"period":{"start":2004, "end":2005}}],
15+
"Bio":"Vlade Divac (Serbian Cyrillic: Владе Дивац) (born February 3, 1968) is a retired Serbian professional basketball player and is currently the vice president of basketball operations and general manager of the Sacramento Kings.[1]. Divac spent most of his career in the National Basketball Association (NBA). At 7 ft 1 in, he played center and was known for his passing skills. Divac was among the first group of European basketball players to transfer to the NBA in the late 1980s and was named one of the 50 Greatest Euroleague Contributors.[2] Divac is one of seven players in NBA history to record 13,000 points, 9,000 rebounds, 3,000 assists and 1,500 blocked shots, along with Kareem Abdul-Jabbar, Tim Duncan, Shaquille O''Neal, Kevin Garnett, Pau Gasol and Hakeem Olajuwon.[3][n 1] Divac was also the first player born and trained outside the United States to play in over 1,000 games in the NBA. On August 20, 2010, Divac was inducted into the FIBA Hall of Fame in recognition of his play in international competition.[4] Aside from being noticed for his basketball abilities, Divac is also known as a humanitarian, helping children in his native country of Serbia, and in Africa.[5] In October 2008 Divac was appointed a government adviser in Serbia for humanitarian issues.[6] In February 2009 he was elected President of the Serbian Olympic Committee for a 4-year term.[7] and re-elected in November 2012,[8] Divac received an honor from the World Sports Humanitarian Hall of Fame.[9], In summer 1986, at 18, right after signing for KK Partizan, Divac debuted for the senior Yugoslavia national basketball team at the 1986 FIBA World Championship in Madrid, on invitation by the head coach Krešimir Ćosić. However, the excellent rookie''s performance was spoiled by the event in the semi-finals against Soviet Union. Forty-five seconds before the end, Yugoslavia had a comfortable lead of 9 points, but Soviets scored two three-pointers within a few seconds and cut the difference to 3 points. Yugoslavia tried to hold the ball for the remaining time, opting to continue the play with throw-ins instead of free throws following fouls, but with only 14 seconds left, Divac committed a double dribble, the Soviets were awarded the ball, and tied the score with another three-pointer. In the overtime, the Soviets easily prevailed against the shocked Yugoslavs, who had to be content with the bronze.[10] The next year, Divac participated in the team that took the gold at the FIBA Junior World Championship (since split into separate under-19 and under-21 events) in Bormio, Italy. That event launched the young generation of Yugoslavian basketballers, also featuring stars like Rađa and Kukoč, regarded as likely the best in history. Before the breakup of Yugoslavia, they would also take the titles at EuroBasket 1989 and the 1990 FIBA World Championship in Argentina,[10] where they were led by Dražen Petrović,[24] as well as the EuroBasket 1991 title, with Aleksandar Đorđević at point guard.[25], Drafted into the NBA in 1989 by the Los Angeles Lakers. He was also one of the first European players to have an impact in the league. Under the mentorship of Kareem Abdul-Jabbar and Magic Johnson, he improved his play and adapted to the American style of the game. Though he spoke no English, he quickly became popular among his teammates and the public for his charm and joviality. In the 1989–90 season, he was selected into the NBA All-Rookie Team.[10] Divac earned a reputation for flopping, or deceiving the officials into calling a foul on the other team by purposely falling to the floor upon contact with an opposing player.[12] Veteran NBA forward P. J. Brown claimed that Divac might have been the best of all time at flopping.[13] Divac freely admitted doing so, adding that he usually did it when he felt like the officials had missed some calls and owed him.[14] Ian Thomsen, a Sports Illustrated columnist, grouped Divac with fellow international players Anderson Varejão and Manu Ginóbili as the players who \"made [flopping] famous\", exaggerating contact on the court in a manner analogous to diving in FIFA games.[15]"
16+
}'
17+
18+
----> Check is @json properly formatted.
19+
--SELECT ISJSON(@json)
20+
21+
22+
----> Get a value from "Name" key
23+
--SELECT JSON_VALUE(@json, '$.Name')
24+
25+
26+
----> Get a value from "Born.Dob" path
27+
-- SELECT JSON_VALUE(@json, '$.Born.DoB')
28+
29+
30+
----> Get a value from "Career[2].period.start" path
31+
--SELECT JSON_VALUE(@json, '$.Career[2].period.start')
32+
33+
34+
----> Use "" if your key has non-alphanumeric characters.
35+
--SELECT JSON_VALUE(@json, '$."NBA Stat".rebounds')
36+
37+
38+
----> JSON path is case sensitive. The following query will return NULL:
39+
--SELECT JSON_VALUE(@json, '$.name')
40+
41+
42+
----> It will fail because $.Bio is bigger than 8K.
43+
---- Without strict it would return null.
44+
--SELECT JSON_VALUE(@json, 'strict $.Bio')
45+
46+
47+
----> JSON_VALUE returns NULL if object is referenced
48+
--SELECT JSON_VALUE(@json, '$.Born')
49+
50+
51+
----> JSON_QUERY returns content of the object
52+
--SELECT JSON_QUERY(@json, '$.Born')
53+
54+
55+
----> JSON_QUERY will fail because $.Name is not an object.
56+
--SELECT JSON_QUERY(@json, 'strict $.Name')
57+
58+
59+
----> JSON_MODIFY updates JSON text with a new value on a specified path.
60+
-- SELECT JSON_MODIFY(@json, '$.Bio', 'Vlade Divac is a retired professional NBA player...')
61+
62+
63+
----> JSON_MODIFY may even append elements in an array.
64+
-- SELECT JSON_MODIFY(@json, 'append $.Teams', 'Charlotte Hornets')
65+
66+
67+
68+
----> Problem: this function will set text
69+
---- "{\"DoB\":\"02/03/1968\",\"Town\":\"Prijepolje\"}"
70+
---- instead of the object {"DoB":"02/03/1968","Town":"Prijepolje"}
71+
-- SELECT JSON_MODIFY(@json, '$.Born', '{"DoB":"02/03/1968","Town":"Prijepolje"}')
72+
73+
74+
----> Solution: use JSON_QUERY to "cast" JSON text to JSON
75+
---- Input text will not be escaped.
76+
-- SELECT JSON_MODIFY(@json, '$.Born', JSON_QUERY('{"DoB":"02/03/1968","Town":"Prijepolje"}'))
77+
78+
79+
80+
----> Get all fields from a JSON:
81+
--SELECT * FROM OPENJSON(@json)
82+
83+
84+
85+
----> Get all Fields from an object in the specified path:
86+
--SELECT * FROM OPENJSON(@json, '$.Born')
87+
88+
89+
----> Get all elements from an array on the specified path:
90+
--SELECT * FROM OPENJSON(@json, '$.Teams')
91+
92+
93+
94+
--SELECT value FROM OPENJSON(@json) WHERE [key] = 'Bio'
95+
96+
97+
98+
-- SELECT Bio FROM OPENJSON(@json) WITH (Bio nvarchar(MAX))
99+
100+
101+
102+
--SELECT * FROM OPENJSON(@json, '$.Born')
103+
-- WITH (DoB datetime2, Town nvarchar(50), Country nvarchar(50))
104+
105+
106+
107+
--SELECT * FROM OPENJSON(@json, '$.Career')
108+
-- WITH (team nvarchar(50), gp int, gs int)
109+
110+
111+
----> Paths in column definitions enable you to parse nested JSON.
112+
--SELECT *
113+
--FROM OPENJSON(@json, '$.Career')
114+
-- WITH (team nvarchar(50), gp int, gs int,
115+
-- StartYear int '$.period.start',
116+
-- EndYear int '$.period.end')
117+
118+
119+
----> AS JSON option returns JSON nested object.
120+
--SELECT *
121+
--FROM OPENJSON(@json, '$.Career')
122+
-- WITH (team nvarchar(50), gp int, gs int,
123+
-- period nvarchar(max) AS JSON)
124+
125+
----> Use CROSS APPLY OPENJSON to parse nested period array on the 2nd level.
126+
--SELECT team, gp, gs, start, [end]
127+
--FROM OPENJSON(@json, '$.Career')
128+
-- WITH (team nvarchar(50), gp int, gs int,
129+
-- period nvarchar(max) AS JSON)
130+
-- CROSS APPLY OPENJSON (period)
131+
-- WITH (start int, [end] int)
132+
133+
134+
135+
136+
--select 1 as x, 2 as y, 3 as z, null as nothing
137+
--for json path;
138+
139+
140+
141+
142+
--select 1 as "point.x", 2 as "point.y", 3 as z
143+
--for json path
144+
145+
146+
147+
--with src(x,y) as (select 1 as x, 2 as y union select 3 as x, 4 as y)
148+
--select * from src
149+
--for json path;
150+
151+
152+
153+
154+
155+
--select 1 as x, 2 as y, 3 as z
156+
--for json path, without_array_wrapper;
157+
158+
159+
160+
161+
162+
--with src(x,y) as (select 1 as x, 2 as y union select 3 as x, 4)
163+
--select * from src
164+
--for json path, without_array_wrapper
165+
166+
167+
168+
--select 1 as x, 2 as y, 3 as z for json path, root('object')
169+
170+
171+
172+
173+
174+
175+
--select 1 as x, 2 as y, 3 as z,
176+
-- JSON_QUERY('{"x":1,"y":2}') as json
177+
--for json path
178+
179+
180+
181+
182+
183+
--select 1 as x, 2 as y, 3 as z,
184+
-- (select 1 as x, 2 as y for json path) as json
185+
--for json path
186+
187+
188+
189+
190+
191+
--select 1 as x, 2 as y, 3 as z,
192+
-- (select 1 as x, 2 as y for json path, without_array_wrapper) as NOjson
193+
--for json path
194+
195+
196+
197+
--select 1 as x, 2 as y, 3 as z,
198+
-- JSON_QUERY((select 1 as x, 2 as y for json path, without_array_wrapper)) as json
199+
--for json path
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)