在sql 2016中有效:
SELECT @JSONFileData = BulkColumn FROM OPENROWSET (BULK
'C:\Temp\Students_2015.json', SINGLE_CLOB) as JSON
这不是:
SELECT @JSONFileData = BulkColumn FROM OPENROWSET (BULK ' + @FullFile + ' ,
SINGLE_CLOB) as JSON
Message: Cannot bulk load. The file " + @FullFile + " does not exist.
@FullFile ='C:\ Temp \ Students_2015.json'
我正在一个SSDT项目的Foreach循环容器中使用Execute SQL任务,该任务将传递许多文件。如何格式化SQL查询,以便可以按参数传递文件?
谢谢
凯文
这是我的考试:
DECLARE @Year varchar(4)
SET @Year = '2015'
DECLARE @Path varchar(50)
SET @Path= 'C:\Temp\'
DECLARE @File varchar(10)
SET @File= 'Students_'
DECLARE @FullFile varchar(50)
SET @FullFile= @Path+@File+@Year+'.json'
Declare @JSONFileData varchar(max)
SELECT @JSONFileData = BulkColumn FROM OPENROWSET (BULK
'C:\Temp\Students_2015.json', SINGLE_CLOB) as JSON
SELECT @JSONFileData = BulkColumn FROM OPENROWSET (BULK ' + @FullFile + ' ,
SINGLE_CLOB) as JSON
答案 0 :(得分:0)
动态SQL为我工作。另外,由于我正在解析JSON文件,因此需要在'$ .xxx'json元素周围添加另一组''以成功查询文件。 这是我的工作示例:
DECLARE @FullFile varchar(50)
SET @FullFile = 'C:\Temp\Students_2016.json'
DECLARE @sqlUpdate varchar(max)
SET @sqlUpdate =
'Declare @JSONFileData varchar(max)
SELECT @JSONFileData = BulkColumn FROM OPENROWSET (BULK N''' + @FullFile + ''',
SINGLE_CLOB) as JSON
INSERT INTO Student_Stage (
FirstName
)
SELECT
JSON_VALUE(sourceTable.value, ''$.FirstName'') AS [FirstName]
FROM OPENJSON(@JSONFileData, ''$.Items'') AS sourceTable'
EXEC (@sqlupdate)