Skip to content

Commit eeb29cb

Browse files
committed
update ApiCovid19
1 parent 5e30006 commit eeb29cb

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

web-call/ApiCovid19.sql

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
/*
2-
3-
*/
1+
--======================================================
2+
-- Usage: ApiCovid19
3+
-- Notes: Practical exercise of API.sql
4+
-- CAUTION!! This is scripted inside with real tables DROP and CREATE
5+
-- History:
6+
-- Date Author Description
7+
-- 2020-06-11 DN Intial
8+
--======================================================
49
DROP PROCEDURE IF EXISTS [dbo].ApiCovid19
510
GO
611
CREATE PROCEDURE [dbo].ApiCovid19 @Url varchar(8000) = 'https://api.covid19api.com',
@@ -195,6 +200,7 @@ BEGIN
195200
WITH (Country nvarchar(255) N'$.Country', Slug nvarchar(255) N'$.Slug', ISO2 varchar(2) N'$.ISO2')
196201

197202
--Crawling countryDayOneRoute
203+
DECLARE @vCountry nvarchar(255)
198204
DROP TABLE IF EXISTS ApiCovid19CountryDayOne
199205
CREATE TABLE ApiCovid19CountryDayOne
200206
(
@@ -211,31 +217,56 @@ BEGIN
211217
Active decimal(17,2),
212218
Date DateTime
213219
)
220+
Declare c CURSOR FOR
221+
SELECT Slug
222+
FROM ApiCovid19Countries
223+
ORDER BY 1
224+
OPEN c
225+
FETCH NEXT FROM c INTO @vCountry
226+
WHILE @@FETCH_STATUS = 0
227+
BEGIN
228+
SELECT TOP 1 @vRoute = @Url + LEFT(Path, CHARINDEX(':',Path,1)-1) + @vCountry
229+
FROM ApiCovid19Route
230+
WHERE RouteName = 'countryDayOneRoute'
231+
PRINT 'GET ' + @vRoute
214232

215-
SELECT TOP 1 @vRoute = @Url + LEFT(Path, CHARINDEX(':',Path,1)-1) + 'vietnam'
216-
FROM ApiCovid19Route
217-
WHERE RouteName = 'countryDayOneRoute'
218-
PRINT 'GET ' + @vRoute
233+
EXEC @vReturnCode = sp_OAMethod @vWin, 'Open', NULL, @Method/*Method*/, @vRoute /*Url*/, 'false' /*IsAsync*/
234+
IF @vReturnCode <> 0 GOTO EXCEPTION
235+
EXEC @vReturnCode = sp_OAMethod @vWin, 'SetRequestHeader', NULL, 'Content-Type', @ContentType
236+
IF @vReturnCode <> 0 GOTO EXCEPTION
237+
EXEC @vReturnCode = sp_OAMethod @vWin,'Send'
238+
IF @vReturnCode <> 0 GOTO EXCEPTION
239+
DELETE FROM @tResponse
240+
INSERT INTO @tResponse (ResponseText)
241+
EXEC @vReturnCode = sp_OAGetProperty @vWin,'ResponseText'
242+
IF @vReturnCode <> 0 GOTO EXCEPTION
243+
SELECT @vResponse = ResponseText
244+
FROM @tResponse
219245

220-
EXEC @vReturnCode = sp_OAMethod @vWin, 'Open', NULL, @Method/*Method*/, @vRoute /*Url*/, 'false' /*IsAsync*/
221-
IF @vReturnCode <> 0 GOTO EXCEPTION
222-
EXEC @vReturnCode = sp_OAMethod @vWin, 'SetRequestHeader', NULL, 'Content-Type', @ContentType
223-
IF @vReturnCode <> 0 GOTO EXCEPTION
224-
EXEC @vReturnCode = sp_OAMethod @vWin,'Send'
225-
IF @vReturnCode <> 0 GOTO EXCEPTION
226-
DELETE FROM @tResponse
227-
INSERT INTO @tResponse (ResponseText)
228-
EXEC @vReturnCode = sp_OAGetProperty @vWin,'ResponseText'
229-
IF @vReturnCode <> 0 GOTO EXCEPTION
230-
SELECT @vResponse = ResponseText
231-
FROM @tResponse
246+
INSERT
247+
INTO ApiCovid19CountryDayOne
248+
SELECT Country,CountryCode,Province,City,CityCode,Lat,Lon,Confirmed,Deaths,Recovered,Active,Date
249+
FROM OPENJSON(@vResponse)
250+
WITH (
251+
Country nvarchar(255) N'$.Country',
252+
CountryCode varchar(10) N'$.CountryCode',
253+
Province nvarchar(255) N'$.Province',
254+
City nvarchar(255) N'$.City',
255+
CityCode nvarchar(255) N'$.CityCode',
256+
Lat decimal(10,7) N'$.Lat',
257+
Lon decimal(10,7) N'$.Lon',
258+
Confirmed decimal(17,2) N'$.Confirmed',
259+
Deaths decimal(17,2) N'$.Deaths',
260+
Recovered decimal(17,2) N'$.Recovered',
261+
Active decimal(17,2) N'$.Active',
262+
Date DateTime N'$.Date'
263+
)
232264

233-
--INSERT
234-
--INTO ApiCovid19CountryDayOne
235-
-- SELECT Country,Slug,ISO2
236-
--FROM OPENJSON(@vResponse)
237-
SELECT @vResponse
238265

266+
FETCH NEXT FROM c INTO @vCountry
267+
END
268+
CLOSE c
269+
DEALLOCATE c
239270

240271
--Dispose objects
241272
IF @vWin IS NOT NULL

0 commit comments

Comments
 (0)