Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql-打开Json插入表_Sql_Json - Fatal编程技术网

Sql-打开Json插入表

Sql-打开Json插入表,sql,json,Sql,Json,我的存储过程中有一部分用于检索从Json接收到的数据,然后我使用OPENJSON以表格式显示 但是,如何将它插入到现有表中,并使用OpenJson中相同的声明列名 Declare @JsonString varchar(MAX) = (Select DataReceived from ItemTransaction where DocType ='LoadItem' and BranchCode = @pBranchCode and TranN

我的存储过程中有一部分用于检索从Json接收到的数据,然后我使用OPENJSON以表格式显示

但是,如何将它插入到现有表中,并使用OpenJson中相同的声明列名

 Declare @JsonString varchar(MAX) = (Select DataReceived from ItemTransaction where 
                        DocType ='LoadItem' and BranchCode = @pBranchCode and TranNo =  @pTranNo)


Sample ReplyData string
'{"status":{"code":"0","name":"RCRD_LOADED","status":"Success","message":"Record sucessfully loaded"},"records":[{"recordtype":"inventoryitem","id":"2310","itemid":"00999","displayname":"Neck - Tie","locationquantityonhand":"9996","locationquantityavailable":"9994","locationquantitycommitted":"9994","locationquantitybackordered":"9994"}]}'


SELECT * FROM  
OPENJSON ( @JsonString,'$."records"')  
WITH (   
              RecordType   varchar(200) '$.recordtype' ,  
              ItemID     varchar     '$.id',  
              ItemDetails varchar(200) '$.itemid',  
              Quantity int          '$.locationquantityonhand'  
 ) 
 end

您可以将具有类似于OPENSJSON模式的表插入其中


创建TestJson表
(
记录类型varchar(200)
,ItemID varchar
,ItemDetails varchar(200)
,数量整数
)
声明@JsonString VARCHAR(最大值)=
“{”status:{”code:“0”,“name:“RCRD_LOADED”,“status:“Success”,“message:“Record successfully LOADED”},“records:“{”recordtype:“inventoryitem”,“id:“2310”,“itemid:“00999”,“displayname:“Neck-Tie”,“locationquantityonhand:“9996”,“locationquantityavailable:“9994”,“locationquantitycommitted:“9994”,“locationquantitybackordered:“9994”}”
插入到TestJson中
从中选择*
OPENJSON(@JsonString,“$”记录“)
与(
RecordType varchar(200)“$.RecordType”,
ItemID varchar“$.id”,
ItemDetails varchar(200)“$.itemid”,
数量int“$.locationquantityonhand”
) 
从TestJson中选择*

+---------------+--------+-------------+----------+
|  RecordType   | ItemID | ItemDetails | Quantity |
+---------------+--------+-------------+----------+
| inventoryitem |      2 |       00999 |     9996 |
+---------------+--------+-------------+----------+