如何使用OPENJSON动态加载SQL表

qlvxas9a  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(113)

我有一个json,看起来像

[{
                "year": 2021,
                "state": "Nebraska",
                "report": "Farm Business Balance Sheet",
                "farmtype": "All Farms",
                "category": "Operator Age",
                "category_value": "45 to 54 years old",
                "category2": "All Farms",
                "category2_value": "TOTAL",
                "variable_id": "kount",
                "variable_name": "Farms",
                "variable_sequence": 10,
                "variable_level": 1,
                "variable_group": null,
                "variable_group_id": null,
                "variable_unit": "Number",
                "variable_description": "Estimated number of farms.",
                "variable_is_invalid": false,
                "estimate": 5889,
                "median": null,
                "statistic": "TOTAL",
                "rse": 0,
                "unreliable_estimate": 0,
                "decimal_display": 0
            },
            {
                "year": 2021,
                "state": "Nebraska",
                "report": "Farm Business Balance Sheet",
                "farmtype": "All Farms",
                "category": "Farm Typology",
                "category_value": "Off-farm occupation farms (2011 to present)",
                "category2": "All Farms",
                "category2_value": "TOTAL",
                "variable_id": "kount",
                "variable_name": "Farms",
                "variable_sequence": 10,
                "variable_level": 1,
                "variable_group": null,
                "variable_group_id": null,
                "variable_unit": "Number",
                "variable_description": "Estimated number of farms.",
                "variable_is_invalid": false,
                "estimate": 13398,
                "median": null,
                "statistic": "TOTAL",
                "rse": 0,
                "unreliable_estimate": 0,
                "decimal_display": 0
            }]

我正在尝试使用OPENJSON动态地将JSON插入到表中。为此,我正在做这样的事情:

CREATE PROCEDURE load_proc 
@json NVARCHAR(MAX),
@table_name VARCHAR(50)
AS 
begin
declare @sql varchar(max)
set @sql= 'INSERT INTO [dbo].' + QUOTENAME(@table_name) + '(year_, state_,
report,farmtype,category,category_value,category2,category2_value,
variable_id,variable_name,variable_sequence,variable_level,
variable_group,variable_group_id,variable_unit,
variable_description,variable_is_invalid,estimate,median,statistic,
rse,unreliable_estimate,decimal_display) SELECT year_,
state_,
report,
farmtype,
category,
category_value,
category2,
category2_value,
variable_id,
variable_name,
variable_sequence,
variable_level,
variable_group,
variable_group_id,
variable_unit,
variable_description,
variable_is_invalid,
estimate,
median,
statistic,
rse,
unreliable_estimate,
decimal_display
        FROM OPENJSON('''+ @json +''',' + '''$'')
            WITH (
            year_ varchar(255) ' + '''$.year'',
state_ varchar(255) ' + '''$.state'',
report varchar(255) ' + '''$.report'',
farmtype varchar(255) ' + '''$.farmtype'',
category varchar(255) ' + '''$.category'',
category_value int ' + '''$.category_value'',
category2 varchar(255) ' + '''$.category2'',
category2_value int ' + '''$.category2_value'',
variable_id varchar(255) ' + '''$.variable_id'',
variable_name varchar(255) ' + '''$.variable_name'',
variable_sequence varchar(255) ' + '''$.variable_sequence'',
variable_level varchar(255) ' + '''$.variable_level'',
variable_group varchar(255) ' + '''$.variable_group'',
variable_group_id varchar(255) ' + '''$.variable_group_id'',
variable_unit varchar(255) ' + '''$.variable_unit'',
variable_description varchar(255) ' + '''$.variable_description'',
variable_is_invalid varchar(255) ' + '''$.variable_is_invalid'',
estimate int ' + '''$.estimate'',
median int ' + '''$.median'',
statistic varchar(255) ' + '''$.statistic'',
rse int ' + '''$.rse'',
unreliable_estimate int ' + '''$.unreliable_estimate'',
decimal_display int ' + '''$.decimal_display'')'
execute(@sql)
end

不幸的是,这对我不起作用,所以我通过运行

SELECT root.[key] AS [Order],TheValues.[key], TheValues.[value]
FROM OPENJSON ( @JSON ) AS root
CROSS APPLY OPENJSON ( root.value) AS TheValues

这给了我

Order   |   key      |   value   
0       |   year     |   2021    
0       |   state    |   Nebraska
0       |   report   |   Farm Business Balance Sheet
0       |   farmtype |   All Farms

select * from openjson(@json)

这给了我

key        |          value                             |         type
0             { "year": 2021, "state": "Nebraska"....               5
1.            { "year": 2021, "state": "Nebraska"...                5

所以似乎我需要调整SP的OPENJSON('''+ @json +''',' + '''$'')部分,但我不确定它应该如何格式化。有人有指导吗?

5kgi1eie

5kgi1eie1#

你的问题在这里:

category_value int ' + '''$.category_value'',
category2 varchar(255) ' + '''$.category2'',
category2_value int ' + '''$.category2_value'',

您已经将category_valuecategory2_value声明为int,但在JSON中,您可以为它们提供字符串值:

"category_value": "45 to 54 years old",
"category2": "All Farms",
"category2_value": "TOTAL",

这导致OPENJSON失效。
纠正这些字段的定义(例如,varchar(255))将使您的代码工作。
dbfiddle.uk上的演示

相关问题