CREATE TABLE [dbo].[Device]
(
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[DeviceStatus] [int] NOT NULL,
[Type] [nvarchar](64) NOT NULL,
[Serial] [nvarchar](64) NOT NULL,
[Group] [nvarchar](max) NULL,
[Name] [nvarchar](max) NULL,
[IP] [nvarchar](max) NULL,
[Description] [nvarchar](max) NULL,
[JsonConfig] [nvarchar](max) NULL,
[JsonStatus] [nvarchar](max) NULL,
[RSSI] [int] NOT NULL,
[DateCreated] [datetime2](7) NOT NULL,
[DateUpdated] [datetime2](7) NOT NULL,
[DateLastSeen] [datetime2](7) NOT NULL,
[BatteryVoltage] [int] NOT NULL,
[IsBatteryPowered] [bit] NOT NULL,
[Uptime] [int] NOT NULL,
[Memory] [int] NOT NULL,
[Version] [nvarchar](max) NULL
)
The JsonConfigJsonConfig
column has this data
I'm recievingreceiving a json document like this :
thisThis is what I have done so far but don't know how to extract each resource[]resource[]
item to insert it's fields to the item [].status[].status
ALTER PROCEDURE sp_process_device_stat
(@json nvarchar(MAX))
AS BEGIN
BEGIN
Update UPDATE device
SET
[type]=SET [type] = Item.[type],
[version]= [version] = Item.ver,
[ip]= [ip] = Item.[ip],
[uptime]= [uptime] = Item.uptime,
[memory]= [memory] = Item.mem,
[rssi]= [rssi] = Item.rssi,
[jsonStatus] = Item.resources,
[DateLastSeen] = getdateGETDATE()
FROM
OPENJSON(@json)
WITH
(
([serial] nvarchar(100),
[type] nvarchar(100),
[ver] nvarchar(100),
[ip] nvarchar(100),
[uptime] nvarchar(100),
[mem] nvarchar(100),
[rssi] nvarchar(100),
[resources] nvarchar(max) as JSON
) as Item
JOIN
[Device] device ON Item.serial=deviceserial = device.serial;
END