Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

SQL Server 2016 has reduced the NoSQL JSON “advantage” over SQL Server

$
0
0
SQL Server 2016 has reduced the NoSQL JSON “advantage” over SQL Server

Microsoft has finally implement support for JSON data in SQL Server 2016 and in Azure SQL Database. In a previous blog post I JSON and the NoSQL “advantage” over SQL Server I discussed which JSON support was missing from SQL Server and I’m very happy to see this much of this functionality coming. Microsoft has done some nice work with JSON support, but sadly I find that is not really as comprehensive as I had hoped.

How to Work with JSON Data in SQL Server 2016

OPENJSON

Converting JSON to rowset data

EX

DECLARE @json NVARCHAR ( MAX ) = N'

{

"id": 1,

"name": "A green door",

"price": 12.50,

"tags": ["home", "green"]

}' ;

SELECT * FROM OPENJSON ( @json );

->

key value type

id 1 2

name A green door 1

price 12.50 2

tags ["home", "green"] 4

FOR JSON

Formatting query results as JSON

EX

SELECT object_id , name

FROM sys . tables

FOR JSON AUTO

->

[{"object_id":117575457,"name":"spt_fallback_db"},{"object_id":133575514,"name":"spt_fallback_dev"},{"object_id":149575571,"name":"spt_fallback_usg"},{"object_id":1483152329,"name":"spt_monitor"},{"object_id":1787153412,"name":"MSreplication_options"}]

ISJSON

Test whether a text string is correctly formatted JSON

EX

DECLARE @json NVARCHAR ( MAX ) = N'

{

"id": 1,

"name": "A green door",

"price": 12.50,

"tags": ["home", "green"]

}' ;

SELECT CASE

WHEN ISJSON ( @json ) > 0

THEN 'The variable value is JSON.'

ELSE 'The variable value is not JSON.'

END ;

->

The variable value is JSON.

JSON_VALUE

Extract a scalar value from a JSON snippet

EX

; WITH CTE AS (

SELECT (

SELECT *

FROM sys . tables

FOR JSON AUTO

) as Result

)

SELECT JSON_VALUE ( CTE . Result , '$[0].name' ) FROM CTE

->

spt_fallback_db

JSON_QUERY

Extract an object or array from a JSON snippet

EX

; WITH CTE AS (

SELECT (

SELECT *

FROM sys . tables


Viewing all articles
Browse latest Browse all 3160

Trending Articles