I am trying to return multiple rows using the following SQL Server 2017 TSQL from JSON
DECLARE @json NVARCHAR(MAX);
SET @json = N'{
"DateCreated": "2020-08-02T16:04:59.3558001+01:00",
"Name": "Bolts",
"Price": 123.45,
"Length": 15.7,
"Quantity": 1,
"Attributes": [
{
"AttrHeading": "Material",
"AttrVal": "Steel"
},
{
"AttrHeading": "Colour",
"AttrVal": "Black"
},
{
"AttrHeading": "Finish",
"AttrVal": "Matt"
}
]
}';
SELECT j.*
FROM OPENJSON (@json, N'$')
WITH (
DateCreated datetime2 N'$.DateCreated',
[Name] varchar(100) N'$.Name',
Price decimal(19,4) N'$.Price',
[Length] decimal(19,4) N'$.Length',
Quantity integer N'$.Quantity',
AttrHeading varchar(max) N'$.Attributes.AttrHeading',
AttrVal varchar(max) N'$.Attributes.AttrVal'
) AS j;
What I need is for three rows to be returned, one row for each of the Attributes. So the Name, Price, Length etc would be the same on each row, just AttrHeading and AttrValue would change.
Currently AttrHeading and AttrVal are null - is it possible to do this?