如何使用多个JSON行创建数据库视图

时间:2018-08-09 13:59:33

标签: json sql-server

我正在尝试创建一个表示以下内容的单独视图:

-> for each row of a table,  create a View Row with the  respective json data in of the single Table's Data. 

我遵循了Microsoft的一些最佳实践,即通过SQL Server进行json转换,但这是将所有Data表转换为单个Json Blob的唯一方法。

SQL查询

 SELECT TOP 2  
    SalesOrderNumber,  
    OrderDate,  
    (SELECT UnitPrice, OrderQty  
      FROM Sales.SalesOrderDetail AS D  
      WHERE H.SalesOrderID = D.SalesOrderID  
     FOR JSON PATH) AS D  
FROM Sales.SalesOrderHeader AS H  
FOR JSON PATH 

JSON嵌套

[{
    "SalesOrderNumber": "SO43659",
    "OrderDate": "2011-05-31T00:00:00",
    "D": [{
        "UnitPrice": 24.99,
        "OrderQty": 1
    }]
}, {
    "SalesOrderNumber": "SO4390",
    "D": [{
        "UnitPrice": 24.99
    }]


}]

示例:

DB TABLE Row N
->Id: 1100
->RecipeName: Spaghetti
->Price : 50$
->Restaurant: the Flower
-> City: Rome

DB VIEW ROW N
-> Id:1
->json:[{"Id": "1100","RecipeName": "Spaghetti","Price ": "50$","Restaurant": "the Flower", "50$","City": "Rome"}]

0 个答案:

没有答案