使用存储过程将API生成的JSON输出数据存储到表中

时间:2019-10-16 07:16:22

标签: sql-server

执行存储过程时,我能够从API检索数据并在SSMS中显示结果。但是我想将数据存储到表中。

Declare @Object as Int;
DECLARE @hr  int
Declare @json as table(Json_Table nvarchar(max))

Exec @hr=sp_OACreate 'MSXML2.ServerXMLHTTP.6.0', @Object OUT;
IF @hr <> 0 EXEC sp_OAGetErrorInfo @Object
Exec @hr=sp_OAMethod @Object, 'open', NULL, 'get',
                 'https://opendata.arcgis.com/datasets/4649ddcede7c4a9289f168c3b2e6e3ee_0.geojson', --Your Web Service Url (invoked)
                 'false'
IF @hr <> 0 EXEC sp_OAGetErrorInfo @Object
Exec @hr=sp_OAMethod @Object, 'send'
IF @hr <> 0 EXEC sp_OAGetErrorInfo @Object
Exec @hr=sp_OAMethod @Object, 'responseText', @json OUTPUT
IF @hr <> 0 EXEC sp_OAGetErrorInfo @Object
INSERT into @json (Json_Table) exec sp_OAGetProperty @Object, 'responseText'
-- select the JSON string
select * from @json
-- Parse the JSON string
SELECT * FROM OPENJSON((select * from @json), N'$.features')
WITH (   
      [OBJECTID] nvarchar(max) N'$.properties.OBJECTID'   ,
      [CONTROL_SECT_JOB]   nvarchar(max) N'$.properties.CONTROL_SECT_JOB',
      [DISTRICT_NUMBER]   nvarchar(max) N'$.properties.DISTRICT_NUMBER',
      [COUNTY_NUMBER]   nvarchar(max) N'$.properties.COUNTY_NUMBER',
      [HIGHWAY_NUMBER]   nvarchar(max) N'$.properties.HIGHWAY_NUMBER',
      [PROJ_CLASS]   nvarchar(max) N'$.properties.PROJ_CLASS',
      [PROJ_CLASS]   nvarchar(max) N'$.properties.geometry.paths'
)
EXEC sp_OADestroy @Object
INSERT into @json (Json_Table) exec sp_OAGetProperty @Object, 'responseText'

我想将@json(Json_Table)存储到其他表中,因为@json(Json_Table)对数据库不可见。

0 个答案:

没有答案