这篇文章主要介绍“sql Server中的JSON函数怎么使用”,在日常操作中,相信很多人在SQL Server中的jsON函数怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SQL
这篇文章主要介绍“sql Server中的JSON函数怎么使用”,在日常操作中,相信很多人在SQL Server中的jsON函数怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SQL Server中的JSON函数怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
下面是我们熟悉的SELECT及输出格式,后面对JSON的演示基于此SQL:
要将SELECT语句的结果以JSON输出,最简单的方法是在后面加上FOR JSON AUTO:
若要为FOR JSON加上Root Key,可以用ROOT选项来自定义ROOT 节点的名称:
若要自定义输出JSON格式的结构时,必须使用JSONPATH。
FOR JSON Auto,自动按照查询语句中使用的表结构来创建嵌套的JSON子数组,类似于For Xml Auto特性。
FOR JSON Path,通过列名或者列别名来定义JSON对象的层次结构,列别名中可以包含“.”,JSON的成员层次结构将会与别名中的层次结构保持一致。
这个特性非常类似于早期SQL Server版本中的For Xml Path子句,可以使用斜线来定义xml的层次结构。
为NULL的数据在输出JSON时,会被忽略,若想要让NULL的字段也显示出来,可以加上选项INCLUDE_NULL_VALUES,该选项也适用于AUTO。
比如下面的SQL,增加了一个“SN”节点,把栏位SERNUM和CLIMAT放在里面:
演示实例:
select TOP (2) id, Plies, Createtime from [dbo].[B3PliesData] ORDER BY ID ;--1178 3 2020-07-21 14:33:18.480--1179 3 2020-07-21 14:36:27.457select TOP (2) id, Plies as [myObject.Plies], Createtime as [myObject.Createtime] from [dbo].[B3PliesData] ORDER BY ID for json auto;--[{"id":1178,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:33:18.480"},{"id":1179,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:36:27.457"}]select TOP (2) id, Plies, Createtime from [dbo].[B3PliesData] ORDER BY ID for json auto ,root('myRoot') ;--{"myRoot":[{"id":1178,"Plies":3,"Createtime":"2020-07-21T14:33:18.480"},{"id":1179,"Plies":3,"Createtime":"2020-07-21T14:36:27.457"}]}select TOP (2) id, Plies as [myObject.Plies], Createtime as [myObject.Createtime] from [dbo].[B3PliesData] ORDER BY ID for json path;--[{"id":1178,"myObject":{"Plies":3,"Createtime":"2020-07-21T14:33:18.480"}},{"id":1179,"myObject":{"Plies":3,"Createtime":"2020-07-21T14:36:27.457"}}]select TOP (2) id, Plies, Createtime,null as mynull from [dbo].[B3PliesData] ORDER BY ID for json path,root('myRoot');--{"myRoot":[{"id":1178,"Plies":3,"Createtime":"2020-07-21T14:33:18.480"},{"id":1179,"Plies":3,"Createtime":"2020-07-21T14:36:27.457"}]}select TOP (2) id, Plies, Createtime,null as mynull from [dbo].[B3PliesData] ORDER BY ID for json path,root('myRoot'),include_null_values;--{"myRoot":[{"id":1178,"Plies":3,"Createtime":"2020-07-21T14:33:18.480","mynull":null},{"id":1179,"Plies":3,"Createtime":"2020-07-21T14:36:27.457","mynull":null}]}
实例演示:
-------------1、-------------declare @json as varchar(8000)set @json='[{"id":1178,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:33:18.480"},{"id":1179,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:36:27.457"}]'select * from openjson(@json);--key value type--0 {"id":1178,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:33:18.480"} 5--1 {"id":1179,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:36:27.457"} 5-------------2、-------------declare @json1 as varchar(8000)set @json1='[{"id":1178,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:33:18.480"},{"id":1179,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:36:27.457"}]'select * from openjson(@json1)with(id varchar(10) '$.id',Plies int '$."myObject.Plies"',Createtime datetime '$."myObject.Createtime"');--id Plies Createtime--1178 3 2020-07-21 14:33:18.480--1179 3 2020-07-21 14:36:27.457-------------3、-------------declare @json2 as varchar(8000)set @json2='{"myRoot":[{"id":1178,"myObject":{"Plies":3,"Createtime":"2020-07-21T14:33:18.480"}},{"id":1179,"myObject":{"Plies":3,"Createtime":"2020-07-21T14:36:27.457"}}]}'select * from openjson(@json2,'$.myRoot')with(id varchar(10) ,Plies int '$.myObject.Plies',Createtime datetime '$.myObject.Createtime');--id Plies Createtime--1178 3 2020-07-21 14:33:18.480--1179 3 2020-07-21 14:36:27.457
declare @param nvarchar(max);set @param = N'{ "info":{ "type":1, "address":{ "town":"Bristol", "county":"Avon", "country":"England" }, "tags":["Sport", "Water polo"] }, "type":"Basic" }';
print iif(isjson(@param) > 0, 'OK', 'NO');
返回:OK
print json_value(@param, '$.info.address.town');print json_value(@param, '$.info.tags[1]');
返回:Bristol,Water polo
print json_query(@param, '$.info');{ "type":1, "address":{ "town":"Bristol", "county":"Avon", "country":"England" }, "tags":["Sport", "Water polo"] }
print json_modify(@param, '$.info.address.town', 'London');
返回:
{ "info":{ "type":1, "address":{ "town":"London", "county":"Avon", "country":"England" }, "tags":["Sport", "Water polo"] }, "type":"Basic" }
实例演示:
declare @param nvarchar(max);set @param=N'{ "info":{ "type":1, "address":{ "town":"Bristol", "county":"Avon", "country":"England" }, "tags":["Sport", "Water polo"] }, "type":"Basic" }';print iif(isjson(@param)>0, 'OK', 'NO');print json_query(@param);print json_value(@param, '$.info.address.town');print json_value(@param, '$.info.tags[1]');print json_query(@param, '$.info');print json_query('["2020-1-8","2020-1-9"]');print json_modify(@param, '$.info.address.town', 'London');
SQL2016 中的新增的内置JSON进行了简单介绍,主要有如下要点:
JSON能在SQLServer2016中高效的使用,但是JSON并不是原生数据类型;
如果使用JSON格式必须为输出结果是表达式的提供别名;
JSON_VALUE 和 JSON_QUERY 函数转移和获取Varchar格式的数据,因此必须将数据转译成你需要的类型。
到此,关于“SQL Server中的JSON函数怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
--结束END--
本文标题: SQL Server中的JSON函数怎么使用
本文链接: https://www.lsjlt.com/news/329912.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
2024-05-12
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0