** Scenario:**
- SQL stored procedure takes fileName as input.
- Read the content of the file and insert it into a table.
For local SQL server, above scenario has been achieved by following stored procedure.
-- @_filePath would be input to the store procedure
DECLARE @_filePath NVARCHAR(MAX) = 'C:\SQLContent\test1.sql'
CREATE TABLE tmpTable (tmpIndex INT NOT NULL IDENTITY PRIMARY KEY CLUSTERED, sqlContent NVARCHAR(MAX) NOT NULL)
-- Read content of the file @_filePath
DECLARE @_insertQuery NVARCHAR(MAX) = 'insert tmpTable (sqlContent) select * from OPENROWSET(bulk ''' + @_filePath + ''', SINGLE_CLOB) as a'
EXEC sp_ExecuteSql @_insertQuery
Select * from tmpTable
Problem:
The actual problem is with Azure SQL server, a path provided in above-mentioned stored procedure won't be accessible to SQL server.
C:\SQLContent\test1.sql
Error Message with SQL Azure:
Cannot bulk load because the file "C:\SQLContent\test1.sql" could not be opened. Operating system error code (null).
Possible Solutions:
- Upload file test1.sql to Azure blob and in the stored procedure read file content from the blob as follows:
select * from OPENROWSET (BULK 'temp1.sql', DATA_SOURCE = 'MyAzureStorage', SINGLE_CLOB) as data
Reference - [error executing OPENROWSET (BULK) / Azure SQL Database
- Instead of passing @_filePath as an argument to the stored procedure, pass file content as an argument to the stored procedure.
Ask
Out of two possible solutions to the problem, which one is the best one and why?
Also, Suggest if there is any other better approach.