I have MS SQL Server (2014) and what I need is to pull a bunch of JSON metadata off a web API. Unfortunately the API is limited to returning only 30 records (and I have 7400 records to pull). So I was going to just iterate over a bunch of URLs and import the data but I got stuck.
I have imported JSON data into SQL Server in the past adapting this method (Passing filename as variable in OPENROWSET(BULK filename)) and using parseJSON
This method works fine if I have the data local to my machine (e.g. on my C drive) but does not work when I use the URL - I get the following error:
(1 row(s) affected)
Msg 4861, Level 16, State 1, Line 1
Cannot bulk load because the file "https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=stackoverflow" could not be opened. Operating system error code 123(The filename, directory name, or volume label syntax is incorrect.).
Msg 8115, Level 16, State 2, Line 45
Arithmetic overflow error converting expression to data type nvarchar.
How can I fix my code to work with a file on the internet?
-- SET @JSON_FILE= 'C:\Temp\SO.json' -- If I save the data on my harddrive the following code works.
SET @JSON_FILE= 'https://api.stackexchange.com/2.2/search?order=desc&sort=activity&intitle=perl&site=stackoverflow'
/* http://www.sqlservercentral.com/Forums/Topic982066-338-1.aspx
Read the JSON file into the varchar variable. This is done via a bulk insert using the OPENROWSET() function. Because this stored proc is to be re-used with different JSON files, ideally you want to pass the JSON file path as a variable. However, because the OPENROWSET() function won't accept variables as a parameter, the command needs to be built as a string and then passed to the sp_executesql system stored procedure. The results are then passed back by an output variable.
*/
-- Setup varchar variable to be used to hold contents of JSON file.
DECLARE @txt varchar(max)
-- The command line
DECLARE @COMMAND NVARCHAR(MAX)
-- The definition of the parameters used within the command line
DECLARE @PARAM_DEF NVARCHAR(500)
-- The parameter used to pass the file name into the command
DECLARE @FILEVAR NVARCHAR(MAX)
-- The output variable that holds the results of the OPENROWSET()
DECLARE @JSON_OUT VARCHAR(MAX)
SET @FILEVAR = @JSON_FILE
SET @PARAM_DEF = N'@JSON_FILE NVARCHAR(MAX), @JSON_OUT VARCHAR(MAX) OUTPUT'
SET @COMMAND = N'SELECT @JSON_OUT = BulkColumn FROM OPENROWSET(BULK ''' + @JSON_FILE + ''', SINGLE_BLOB) ROW_SET';
EXEC sp_executesql @COMMAND, @PARAM_DEF, @JSON_FILE = @FILEVAR, @JSON_OUT = @txt OUTPUT;
--parseJSON function from https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-server/
SELECT * FROM parseJSON(@txt)