SQL SERVER:将查询结果导出为JSON对象

时间:2015-10-27 20:51:51

标签: sql-server json azure-sql-database

我正在使用Azure sql server并尝试以下列格式导出查询结果。

必需的查询结果:

  

{"结果":[{...},{...}],"回复":0}

从此示例中:https://msdn.microsoft.com/en-us/library/dn921894.aspx

我正在使用这个sql,但我不确定如何将另一个响应属性添加为root属性的兄弟:"结果"。

当前查询:

SELECT name, surname
FROM emp
FOR JSON AUTO, ROOT('results')

查询输出:

  

{"结果":[        {"姓名":" John","姓":" Doe" },        {"姓名":"简","姓":" Doe" }]}

2 个答案:

答案 0 :(得分:3)

使用FOR JSON AUTO代替 notifications = ($http.get(link) .success(function(data, status, headers, config){ console.log(data); return data; }) .error(function(err) { return err; }) ); return notifications; 。有关几个示例,请参阅Format Query Results as JSON with FOR JSON (SQL Server)页面,包括点分隔列名称和来自SELECTS的查询

答案 1 :(得分:1)

此格式没有内置选项,因此最简单的方法可能是手动格式化响应,例如:

declare @resp nvarchar(20) = '20'
SELECT '{"response":"' +
         (SELECT * FROM emp FOR JSON PATH) +
         '", "response": ' + @resp + ' }' 

FOR JSON会做更难的部分(格式化表格),你只需要把它包起来。

相关问题