我想将JSON文件的数据保存在SQL表的本地路径中。
存在JSON文件的我的本地路径为E:\ 20180824LocalDump.json
我已经在SQL中使用以下代码从JSON文件中获取数据,但出现错误。
DECLARE @Details VARCHAR(MAX)
SELECT @Details = BulkColumn FROM OPENROWSET(BULK 'E:\20180824LocalDump.json', SINGLE_BLOB) JSON;
SELECT * FROM OPENJSON(@Details)
WITH(Departure_airport nvarchar(50) ,
DisplayName nvarchar(40),
Email nvarchar(60),
Keep_me_deals nvarchar(40),
Phone_Code nvarchar(50),
Provider nvarchar(50),
SignUpDate nvarchar(50),
Telephone nvarchar(50),
[Platform] nvarchar(50),
AppVersion nvarchar(40))
创建了一个表格,如下所示:
Create Table Dump
(
Departure_airport nvarchar(50),
DisplayName nvarchar(40),
Email nvarchar(60),
Keep_me_deals nvarchar(40),
Phone_Code nvarchar(50),
Provider nvarchar(50),
SignUpDate nvarchar(50),
Telephone nvarchar(50),
[Platform] nvarchar(50),
AppVersion nvarchar(40)
)
我的Json文件内容:
[{"Departure_airport":"Test","DisplayName":"Test","Email":"Tst","Keep_me_deals":"Test","Phone_Code":"Test","Provider":"Test","SignUpDate":"Test","Telephone":"Test","Platform":"Test","AppVersion":"Test"},{"Departure_airport":"Test","DisplayName":"Test","Email":"Tst","Keep_me_deals":"Test","Phone_Code":"Test","Provider":"Test","SignUpDate":"Test","Telephone":"Test","Platform":"Test","AppVersion":"Test"},{"Departure_airport":"Test for IE","DisplayName":"Test for IE","Email":"Tst for IE","Keep_me_deals":"Test for IE","Phone_Code":"Test for IE","Provider":"Test for IE","SignUpDate":"Test for IE","Telephone":"Test for IE","Platform":"Test for IE","AppVersion":"Test for IE"},{"Departure_airport":"Test for UK","DisplayName":"Test for UK","Email":"Tst for UK","Keep_me_deals":"Test for UK","Phone_Code":"Test for UK","Provider":"Test for UK","SignUpDate":"Test for UK","Telephone":"Test for UK","Platform":"Test for UK","AppVersion":"Test for UK"},{"Departure_airport":"Test for UK 1","DisplayName":"Test for UK 1","Email":"Tst for UK 1","Keep_me_deals":"Test for UK 1","Phone_Code":"Test for UK 1","Provider":"Test for UK 1","SignUpDate":"Test for UK 1","Telephone":"Test for UK 1","Platform":"Test for UK 1","AppVersion":"Test for UK 1"}]
我遇到类似以下错误:
SQL中无效的对象名称'OPENJSON'。
请帮助我
答案 0 :(得分:1)
如果您使用的是SQL 2016或更高版本,则还应该检查运行查询的数据库的兼容性级别。
main
如果它不是至少130,则需要(在您的DBA的许可下)更改它。
USE <your database>;
GO
SELECT compatibility_level FROM sys.databases WHERE name = '<your database>';
GO
您可能想让Google获得更改兼容性级别的其他效果,但这从来没有给我造成任何问题。
答案 1 :(得分:0)
不幸的是,您不能在SQL Server 2014中使用openjson
:SQL Server 2016引入了本机json函数(有关openjson here的更多信息)。
链接页面的摘录:
适用于:服务器(从2016年开始)
您仍然可以使用OPENROWSET
导入json文件,但是您将不得不依靠字符串操作函数或自定义函数从json文件中提取数据。