我对 OPENJSON 没有太多经验,我尝试使用这些示例,但我无法解析下面的字符串
任何人都可以帮助我。
我希望它采用以下格式:
Date,TimeSheet.StartTime,TimeSheet.FinishTime,Timesheet.BreakStart,TimeSheet.BreakFinish,Timesheet.EmployeeId,TimeSheet.Notes
[
{
"Date": "0001-01-01T00:00:00+00:00",
"TimeSheet": [
{
"StartTime": "0001-01-01T00:00:00+00:00",
"FinishTime": "0001-01-01T00:00:00+00:00",``
"BreakTimes": [
"0001-01-01T00:00:00+00:00",
"0001-01-01T00:00:00+00:00"
],
"EmployeeId": 1,
"Notes": "Blah, blah, blah..."
}
]
},
{
"Date": "0001-01-01T00:00:00+00:00",
"TimeSheet": [
{
"StartTime": "0001-01-01T00:00:00+00:00",
"FinishTime": "0001-01-01T00:00:00+00:00",
"BreakTimes": [
"0001-01-01T00:00:00+00:00",
"0001-01-01T00:00:00+00:00"
],
"EmployeeId": 1,
"Notes": "Blah, blah, blah..."
}
]
}
]
这是我尝试使用的代码:
DECLARE @json NVARCHAR(MAX)
DECLARE @json2 NVARCHAR(MAX)
SET @json= (Select [TimeSheetTestData] FROM [TimeSheetTestData] WHERE [TimeSheetTestDataId] = 3) -- this is the sample json above
SET @json2=(SELECT Value
FROM OpenJson(@json) where [Key] = 'TimeSheet')
--SELECT [Key], Value
--FROM OpenJson(@json2)
SELECT * FROM
OPENJSON ( @json2 )
WITH (
startTime varchar(200) '$.StartTime' ,
finishTime varchar(200) '$.FinishTime' ,
breakTimes varchar(200) '$.BreakTimes' ,
employeeId varchar(200) '$.EmployeeId' ,
notes varchar(200) '$.Notes'
)