我有一个FOR JSON
表达式的结果,我想将它插入到一个表格列中。
我怎么做?
这是我的一次不成功的试验(请注意,该SELECT hightrees ... FOR JSON PATH
部分是正确的,可以在我的另一个问题的答案中看到,这也是现场演示),它给出了错误Invalid object name 'TreesJson'
:
SELECT * FROM (
SELECT
highTrees = JSON_QUERY(
(
SELECT
Id as id,
Type as type,
Height as height
FROM Trees WHERE [Height] > 5
FOR JSON PATH
)
),
lowTrees = JSON_QUERY(
(
SELECT
Id as id,
Type as type,
Height as height
FROM Trees WHERE [Height] < 1
FOR JSON PATH
)
)
FOR JSON
PATH, WITHOUT_ARRAY_WRAPPER
) AS TreesJson;
INSERT INTO TreesGrowthLog ([Day], [TreesGrowth])
VALUES (CAST(GETDATE() AS Date, (SELECT * FROM TreesJson FOR JSON AUTO))
这是一种方法:
以价值观的方式
并使用 CTE 版本:
或使用临时表#