0% found this document useful (0 votes)
8 views4 pages

Chapter29 Examples

The document provides examples of working with JSON data in SQL Server, including creating tables, inserting JSON data, and querying JSON values. It demonstrates how to use functions like OPENJSON and JSON_VALUE to extract information from JSON objects, as well as how to update JSON data using JSON_MODIFY. Various examples illustrate the manipulation and retrieval of JSON data within a SQL context.

Uploaded by

riyasathsafran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Chapter29 Examples

The document provides examples of working with JSON data in SQL Server, including creating tables, inserting JSON data, and querying JSON values. It demonstrates how to use functions like OPENJSON and JSON_VALUE to extract information from JSON objects, as well as how to update JSON data using JSON_MODIFY. Various examples illustrate the manipulation and retrieval of JSON data within a SQL context.

Uploaded by

riyasathsafran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Example 29.

{ "info": {
"who": "Fred" ,"where": "Microsoft"
,"friends": [{ "name": "Lili", "rank": 5}
,{"name": "Hank", "rank": 7}]} }
Example 29.2

USE sample;
CREATE TABLE json_table
(id INT PRIMARY KEY IDENTITY, person_and_friends NVARCHAR(2000));
-- Insert a couple of rows
INSERT INTO json_table (person_and_friends) VALUES
(N' {"info":
{"who": "Fred" ,"where": "SQL Server" ,
"friends": [{ "name": "Lili", "rank": 5} ,{"name": "Hank", "rank": 7}]}}');
INSERT INTO json_table (person_and_friends) VALUES
(N' {"info":{"who": "Tom", "where": "IBM", "friends": [ { "name": "Sharon", "rank": 2},
{"name": "Monty", "rank": 3} ] }}');
INSERT INTO json_table (person_and_friends) VALUES
(N' {"info":{ "who": "Jack", "friends": [ { "name": "Connie" } ] }}');
INSERT INTO json_table (person_and_friends) VALUES
(N' {"info":{ "who": "Joe", "friends": [ { "name": "Doris" }, {"rank": 1} ] }}')
INSERT INTO json_table (person_and_friends) VALUES
(N' {"info": {"who":"Mabel","where":"PostgresSQL","friends":[{"name":"Buck","rank":
6}]}}');
INSERT INTO json_table (person_and_friends) VALUES
(N' {"info":{"who": "Louise", "where": "Hanna" }}');
Example 29.3

DECLARE @json NVARCHAR(MAX) =


N' {"info":{
"who": "Fred" ,"where": "SQL Server" ,
"friends": [{ "name": "Lili", "rank": 5} ,{"name": "Hank", "rank": 7}]}'
SELECT [key], value FROM OPENJSON(@json, N' $.info.friends');
Example 29.4

USE sample;
SELECT dept_no, dept_name
FROM department FOR JSON AUTO;
Example 29.5

USE sample;
SELECT dept_no AS [Department.Number], dept_name AS [Department.Name]
FROM department
FOR JSON PATH, ROOT ('Departments');
Example 29.6

USE sample;
SELECT id, person_and_friends
FROM json_table
WHERE ISJSON(person_and_friends) > 0;
Example 29.7

USE sample;
SELECT Id, JSON_VALUE(person_and_friends, '$.info.where') AS project
FROM json_table
WHERE ISJSON(person_and_friends) > 0
AND JSON_VALUE(person_and_friends, '$.info.who') = 'Fred';
Example 29.8

USE sample;
SELECT id, person_and_friends,
JSON_QUERY(person_and_friends, '$.info.where')
FROM json_table;
Example 29.9

CREATE TABLE json_update_table


(id INT PRIMARY KEY IDENTITY, person_and_friends NVARCHAR(2000));
-- Insert a row
INSERT INTO json_update_table (person_and_friends) VALUES
(N'{"info":{"who": "Fred" , "friends":["Lili", "Hank"]}}');
Example 29.10

-- Update the value of the object


UPDATE json_update_table
SET person_and_friends = json_modify(person_and_friends,
'$.info.who', 'Peter')
WHERE id = 1;
Example 29.11

UPDATE json_update_table
SET person_and_friends= json_modify(person_and_friends,
'append $.info.friends', 'Wendy')
WHERE id = 1;
Example 29.12

-- INSERT a new OBJECT


UPDATE json_update_table
SET person_and_friends = json_modify(person_and_friends,
'$.info.surname', 'Birch')
WHERE id = 1;
Example 29.13

UPDATE json_update_table
SET person_and_friends = json_modify(person_and_friends,
'$.info.surname', NULL)
WHERE id = 1;
Example 29.14

UPDATE json_update_table
SET person_and_friends = json_modify(person_and_friends,
'$.info.friends', NULL)
WHERE id = 1;
UPDATE json_update_table
SET person_and_friends = json_modify (person_and_friends,
'$.info.friends', 'Lili, Wendy,Hank')
WHERE id = 1;

You might also like