I try to insert order information and order lines with open json.
Here is example json.
declare @json nvarchar(max) = '
{"OrderID":0,
"CustomerID":2250,
"SupplierID":1,
"CuratorID":988,
"FirmID":null,
"OrderStateID":2,
"DeliveryTypeID":1,
"Code1C":"",
"CreationDate":"2017-11-12T01:05:14.3233187+03:00",
"ReservationDate":"2017-11-15T01:05:16.1306759+03:00",
"AssemblyDate":null,
"DeliveryDate":"2017-11-12T01:05:15.1748244+03:00",
"TransportAddress":"",
"TransportName":"","TransportPhone":"","RecieverAddress":"","RecieverName":"",
"RecieverPhone":"","Comment":"","LoaderID":null,"DriverID":null,
"CanDelete":true,"LoadingDate":null,"RealizeNum":"","IsDocumentsCreate":false,"DeliveryType":null,"Firm":null,
"OrderLines":[
{"OrderLineID":0,"OrderID":0,"ProductID":1,"ProductCount":100,"ChangeCount":0,"Price":363.0,"Product":null,"Order":null},
{"OrderLineID":0,"OrderID":0,"ProductID":3,"ProductCount":100,"ChangeCount":0,"Price":860.0,"Product":null,"Order":null}
]}'
Now I insert main info in Orders table.
insert into Sales.Orders (CustomerID
,SupplierID
,CuratorID
,OrderStateID
,FirmID
,DeliveryTypeID
,Code1C
,CreationDate
,ReservationDate
,DeliveryDate) select * from openjson(@s) with
(CustomerID int, SupplierID int,
CuratorID int, OrderStateID int, FirmID int, DeliveryTypeID int, Code1C nvarchar(10),
CreationDate datetime2, ReservationDate datetime2, DeliveryDate datetime2);
select * from openjson(@s);
It works fine. The next step I need to insert order lines array. But before insert I need to set OrderID propery to each order line.
Here I select last inserted order identity.
declare @ident int;
select @ident = SCOPE_IDENTITY();
But I don't understand, how to set this value to each order line. The next code doesn't work
set @json = JSON_MODIFY(@s, '$.OrderLines.OrderID', @ident);
Thanks for any advices