我正在寻找使用动态SQL查询直接从(T)SQL创建嵌套JSON字符串的最佳解决方案。
在SQL Server 2016中,使用如下语句创建一个扁平的JSON字符串很容易:
SELECT *
FROM tblTableName
FOR JSON AUTO
如果您需要更复杂的嵌套结果,可以使用简单的递归例程,如:
CREATE FUNCTION [dbo].[NestedJSON](@Id uniqueidentifier)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @Json NVARCHAR(MAX) = '{}';
IF @Id Is NULL
SET @Json =
(SELECT *, JSON_QUERY(dbo.NestedJSON(Id) ) AS child
FROM dbo.tblTableName
WHERE IDParent is NULL
FOR JSON AUTO);
ELSE
SET @Json =
(SELECT *, JSON_QUERY(dbo.NestedJSON(Id) ) AS child
FROM dbo.tblTableName
WHERE IDParent = @Id
FOR JSON AUTO);
RETURN @Json
END
Id 是tblTableName
中每条记录的IDIDParent 是tblTableName中记录的父ID
此递归函数仅在SQL-Query已修复时才有效。
在我的情况下,我有很多带有嵌套结构的查询。为了支持所有许多嵌套的SQL查询,我尝试修改上面的NestedJSON函数,但接缝是不允许在函数中使用动态SQL。我尝试了以下选项:
IF @Id Is NULL
Set @SQL = 'SELECT @Json=(SELECT ' + @FieldList + ' ,JSON_QUERY(dbo.MenuNested(' + @Id + ') ) AS Child FROM ' + @TheTables + ' WHERE IDParent is NULL FOR JSON AUTO)'
ELSE
Set @SQL = 'SELECT @Json=(SELECT ' + @FieldList + ' ,JSON_QUERY(dbo.MenuNested(' + @Id + ') ) AS Child FROM ' + @TheTables + ' WHERE IDParent = ' + @Id + ' FOR JSON AUTO)'
Exec(@SQL)
--or
execute sp_executesql @SQL;
但所有修改都会导致相同的错误:"只能在函数内执行函数和一些扩展存储过程。"
我从vb.net调用SQL服务器,所以我可以创建一个额外的函数来嵌套嵌套的JSON,但这是我的最后一个选项。我认为禁食和最干净的解决方案是在(T)SQL中完成嵌套。
那么,是否有人可以帮助我创建一个能够支持Dynamic的解决方案 SQL并返回一个嵌套的JSON?
感谢任何帮助表示赞赏。
阿诺
答案 0 :(得分:0)
感谢sepupic的答案。
我将函数从最初的帖子转换为存储过程,但我正在努力转换这部分:
SET @Json =
(SELECT *, JSON_QUERY(dbo.NestedJSON(Id)) AS child
FROM dbo.tblTableName
WHERE IDParent is NULL
FOR JSON AUTO);
存储过程不能在查询定义本身中使用,因为它不会以相同的方式返回返回值。因为它无法在查询本身中使用,所以我无法传递参数Id(dbo.NestedJSON(Id))。
我能想到的唯一解决方案是循环所有查询结果并逐个获取每个ID,然后执行它。
还有其他建议吗?
Arno
答案 1 :(得分:0)
我终于解决了这个问题。对于那些感兴趣的人来说,解决方案是:
第一步是创建一个可以在递归例程中使用的新表类型:
CREATE TYPE [dbo].[JSONCTETableType] AS TABLE(
[Sequence] [uniqueidentifier] NULL,
[ParentSequence] [uniqueidentifier] NULL,
[JSON] [nvarchar](max) NULL,
[IndentLevel] [int] NULL,
[WBS] [nvarchar](512) NULL,
[ChildCount] [int] NULL
)
在第二步中,我更改了原始递归函数以使用此表定义:
-- =============================================
-- Author: Arno Voerman
-- Create date: 2017, September
-- =============================================
CREATE FUNCTION [dbo].[TREEIFY]
(
@ParentId uniqueidentifier, @JSONTABLE JSONCTETableType READONLY
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @Json NVARCHAR(MAX);
If @ParentId Is NULL
Begin
SET @Json = (
Select JSON +
Case When ChildCount > 0
THEN ',"children":[' + dbo.treeIfy(Sequence,@JSONTABLE) + ']}'
ELSE ',"leaf":true}'
END + ',' AS 'data()'
FROM @JSONTABLE
Where ParentSequence Is Null
FOR XML PATH('')
)
END
Else
Begin
SET @Json = (
Select JSON +
Case When ChildCount > 0
THEN ',"children":[' + dbo.treeIfy(Sequence,@JSONTABLE) + ']}'
ELSE ',"leaf":true}'
END + ',' AS 'data()'
FROM @JSONTABLE
Where ParentSequence = @ParentId
FOR XML PATH('')
)
End
RETURN @Json
END
要从查询中获取嵌套的JSON结果,必须将查询结果转换为JSONCTETableType。这在以下CTE中完成,它还生成JSON和我的程序需要的一些额外数据:
DECLARE @JSONTT as JSONCTETableType;
WITH JSONCTETable AS (
SELECT Sequence, IDParentSequence As ParentSequence,
(SELECT * FROM YourTable Where Sequence=anchor.Sequence FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER) As JSON,
0 as IndentLevel,
Cast(ROW_NUMBER() OVER(ORDER BY mnu_Order ASC) As NVARCHAR(512)) AS WBS
FROM YourTable AS anchor
WHERE IDParentSequence is null
UNION ALL -------------------------------------------------------------------
SELECT recur.Sequence, recur.IDParentSequence As ParentSequence,
(SELECT * FROM YourTable Where Sequence=recur.Sequence FOR JSON AUTO, WITHOUT_ARRAY_WRAPPER) As JSON,
cte.IndentLevel + 1,
Cast(cte.WBS + '.' + Cast(ROW_NUMBER() OVER(ORDER BY mnu_Order ASC) As NVARCHAR(10)) AS NVARCHAR(512)) AS WBS
FROM YourTable AS recur
INNER JOIN JSONCTETable AS cte ON cte.Sequence = recur.IDParentSequence
)INSERT INTO @JSONTT (Sequence, ParentSequence, JSON, IndentLevel, WBS, ChildCount)
SELECT *, (Select count(*) From JSONCTETable tmpCTE Where tmpCTE.ParentSequence=JSONCTETable.Sequence) as ChildCount
FROM JSONCTETable
ORDER BY WBS;
--Remove last character ("}") of all JSON. Needed to simplify the add "CHILD"
Update @JSONTT SET JSON = stuff(JSON,len(JSON),1,'');
最后一步很简单:
Select '[' + dbo.treeIfy(Null,@JSONTT) + ']' as data, (Select count(Sequence) From @JSONTT) as totRows;
祝你好运!
阿诺