我想将 SELECT 语句的结果输出为 JSON 对象。
id name active Status
1 Bob Jones 1 Active
2 John Smith 0 Inactive
我需要这样的返回结果:
{"Active" :[{"id":1,"name":"Bob Jones" ,"active":1}],
"InActive":[{"id":2,"name":"John Smith","active":0}]}
如何使用查询?
我正在使用此代码
DECLARE @JSONTierDetails AS TABLE (TierDetails VARCHAR(8000))
INSERT INTO @JSONTierDetails
SELECT
(SELECT id, name, active
FROM MyTable
WHERE Status = 'Active'
FOR JSON PATH, INCLUDE_NULL_VALUES) TierDetails
SELECT TierDetails
FROM @JSONTierDetails
提前致谢