I want to read a large number of JSON files (about 1000) from Azure Blob Storage and insert them into a Azure SQL Database SQL table, along the lines of this tutorial but by reading the JSON from a file rather than inline. To be clear: I want each file to be read as a string that becomes a single NVARCHAR(max) entry in the table. This seems like it should be straightforward, but I cannot figure out how to do it.
Below is my code, which yields an error "Incorrect syntax near '('" at the line I have commented. Each JSON file is simply a dictionary, all with the same keys, along the lines of
{"D": "0.980287579", "B": "0.679793971", "C": "0.217004033", "A": "0.675980258"}
IF OBJECT_ID(N'dbo.testJSON', N'U') IS NOT NULL
DROP TABLE dbo.testJSON
GO
CREATE TABLE dbo.testJSON (
rowID smallint NOT NULL PRIMARY KEY,
jsonData nvarchar(max)
)
GO
ALTER TABLE dbo.testJSON
ADD CONSTRAINT [Check for JSON] CHECK (ISJSON(jsonData)=1)
GO
DECLARE @i INT = 1
DECLARE @json AS NVARCHAR(MAX)
DECLARE @file AS VARCHAR(4000)
WHILE @i < 1000
BEGIN
SET @file = 'JSONfile' + CAST(@i AS VARCHAR(5)) + '.json'
-- Line below doesn't work
SELECT @json = BulkColumn FROM OPENROWSET(BULK(@file), SINGLE_CLOB) AS j
-- Line above doesn't work
INSERT INTO dbo.testJSON (rowID, jsonData) VALUES (@i, @json)
set @i = @i + 1
END
GO
Thanks for y'all's help!