在存盤程序中,我有一個for json節點(框):
select
(
select
os.Name,
os.Address,
ss.carrierCode,
(
select
ob.envelopeCode,
ob.boxNumber,
ob.weight,
ob.width,
ob.length,
ob.height
from OrdersBoxes ob
...
where os.OID=ob.OID
...
for json path
) boxes,
....
for json path
) orderDetails
這樣我正確地得到:
"boxes":[{
"envelopeCode":"E70345D2AB90A879D4F53506FB465086",
"boxNumber":1,
"weight":3000,
"width":300,
"length":300,
"height":100
}]
現在我需要從 2 個表中獲取詳細資訊,因此我將使用union命令,將 2 包裝select在另一個select查詢中以避免以下錯誤:
FOR XML 和 FOR JSON 子句在視圖、行內函式、派生表和子查詢中包含集合運算子時無效。要解決此問題,請使用派生表或公用表運算式或視圖包裝包含集合運算子的 SELECT,并在其上應用 FOR XML 或 FOR JSON。
并添加JSON_QUERY以避免轉義嵌套節點:
select
(
select
*
from
(
select
os.Name,
os.Address,
ss.carrierCode,
JSON_QUERY((
select
ob.envelopeCode,
ob.boxNumber,
ob.weight,
ob.width,
ob.length,
ob.height
from OrdersBoxes ob
...
where os.OID=ob.OID
...
for json path
)) boxes,
....
from table1
where....
union
select
os.Name,
os.Address,
ss.carrierCode,
JSON_QUERY((
select
ob.envelopeCode,
ob.boxNumber,
ob.weight,
ob.width,
ob.length,
ob.height
from OrdersBoxes ob
...
where os.OID=ob.OID
...
for json path
)) boxes,
....
from table2
where....
) jj
for json path
) orderDetails
這有效,但 box 節點回傳轉義:
"boxes":"[{\"envelopeCode\":\"E70345D2AB90A879D4F53506FB465086\",\"boxNumber\":1,\"weight\":3000,\"width\":300,\"length\":300,\"height\":100}]"
我也嘗試過這個解決方案,但只有從 1 個表回傳資料時它才能正常作業:
因為它回傳objects {}獲取陣列需要更改第一行
select STRING_AGG (order_details,',') ods from (
到
select concat('[',STRING_AGG (order_details,','),']') ods from (
雖然它有效,但我似乎不是很“優雅”。
有人可以建議一種更好的方法來正確格式化所有資料(因此是未轉義的框節點)?
uj5u.com熱心網友回復:
有關JSON_QUERY() 說明的檔案:... JSON_QUERY 回傳有效的 JSON 片段。因此,FOR JSON 不會轉義 JSON_QUERY 回傳值中的特殊字符。如果您使用 FOR JSON 回傳結果,并且您包含已經采用 JSON 格式的資料(在列中或作為運算式的結果),請使用不帶路徑引數的 JSON_QUERY 包裝 JSON 資料。. 因此,如果我正確理解架構,您需要使用JSON_QUERY()不同的方式:
表格:
SELECT *
INTO table1
FROM (VALUES
(1, 'Name1', 'Address1')
) v (oid, name, address)
SELECT *
INTO table2
FROM (VALUES
(2, 'Name2', 'Address2')
) v (oid, name, address)
SELECT *
INTO OrdersBoxes
FROM (VALUES
(1, 'E70345D2AB90A879D4F53506FB465086', 1, 3000, 300, 300, 100),
(2, 'e70345D2AB90A879D4F53506FB465086', 2, 3000, 300, 300, 100)
) v (oid, envelopeCode, boxNumber, weight, width, length, height)
陳述:
select Name, Address, JSON_QUERY(boxes) AS Boxes
from (
select
os.Name,
os.Address,
(
select ob.envelopeCode, ob.boxNumber, ob.weight, ob.width, ob.length, ob.height
from OrdersBoxes ob
where os.OID = ob.OID
for json path
) boxes
from table1 os
union all
select
os.Name,
os.Address,
(
select ob.envelopeCode, ob.boxNumber, ob.weight, ob.width, ob.length, ob.height
from OrdersBoxes ob
where os.OID = ob.OID
for json path
) boxes
from table2 os
) j
for json path
作為附加選項,您可以嘗試使用FOR JSON AUTO(JSON 輸出的格式是根據SELECT串列中列的順序及其源表自動確定的):
SELECT
cte.Name, cte.Address,
boxes.envelopeCode, boxes.boxNumber, boxes.weight, boxes.width, boxes.length, boxes.height
FROM (
SELECT oid, name, address FROM table1
UNION ALL
SELECT oid, name, address FROM table2
) cte
JOIN OrdersBoxes boxes ON cte.oid = boxes.oid
FOR JSON AUTO
結果:
[
{
"Name":"Name1",
"Address":"Address1",
"boxes":[{"envelopeCode":"E70345D2AB90A879D4F53506FB465086","boxNumber":1,"weight":3000,"width":300,"length":300,"height":100}]
},
{
"Name":"Name2",
"Address":"Address2",
"boxes":[{"envelopeCode":"e70345D2AB90A879D4F53506FB465086","boxNumber":2,"weight":3000,"width":300,"length":300,"height":100}]
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/393005.html
標籤:json sql-server 逃跑 联盟 sql-server-2017
上一篇:兩個日期之間的SQL總和數量
