我正在尝试将一行数据转换为包含单个对象的 JSON 数组。其中一列包含 XML,它概述了转换为数组的另一个单个对象。
我的查询:
WITH r AS (
SELECT TOP 1 * FROM Table1
ORDER BY RecordID ASC)
SELECT
NEWID() AS 'Report.ReportUUID',
Value1 as 'Report.Value1',
Value2 as 'Report.Value2',
DateTime as 'Report.DateTime',
UserID as 'Report.UserID',
'Medium' as 'Report.Priority',
NEWID() as 'Report.Item.ItemUUID',
XML.value('category[1]', 'varchar(100)') as 'Report.Item.Category',
XML.value('description[1]', 'varchar(1000)') as 'Report.Item.Description',
XML.value('date[1]', 'varchar(100)') AS 'Report.Item.DateTime'
FROM r
FOR JSON PATH, ROOT('DataSet');
Run Code Online (Sandbox Code Playgroud)
期望的输出:
{
"DataSet" : {
"Report" : [
{
"ReportUUID" : "uuid here",
"Value1" : "value1",
"Value2" : "value2",
"DateTime" : …Run Code Online (Sandbox Code Playgroud)