Sql-打开Json插入表
我的存储过程中有一部分用于检索从Json接收到的数据,然后我使用OPENJSON以表格式显示 但是,如何将它插入到现有表中,并使用OpenJson中相同的声明列名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
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 |
+---------------+--------+-------------+----------+