mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
Merge pull request #212 from JocaPC/master
Fixed scripts in json/react/nodejs sample
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
select *
|
||||
from Comments
|
||||
for json path
|
||||
|
||||
select *
|
||||
from Comments
|
||||
where id = 1
|
||||
for json path, without_array_wrapper
|
||||
|
||||
declare @p nvarchar(4000) = N'[{"author":"John","text":"I like it too!"},{"author":"Jane","text":"Thanks!"},{"author":"Jane","text":"Buy :)"}]'
|
||||
|
||||
select *
|
||||
from openjson(@p)
|
||||
with ( author nvarchar(20), text nvarchar(200))
|
||||
|
||||
go
|
||||
|
||||
declare @p nvarchar(4000) = N'[{"author":"John","text":"I like it too!"},{"author":"Jane","text":"Thanks!"},{"author":"Jane","text":"Buy :)"}]'
|
||||
|
||||
insert into Comments(author, text)
|
||||
select *
|
||||
from openjson(@p)
|
||||
with ( author nvarchar(20), text nvarchar(200))
|
||||
|
||||
select *
|
||||
from Comments
|
||||
|
||||
select author, count(*) comments
|
||||
from Comments
|
||||
group by author
|
||||
for json path
|
||||
|
||||
delete Comments where id > 2
|
||||
@@ -0,0 +1,15 @@
|
||||
DROP LOGIN WebLogin
|
||||
CREATE LOGIN WebLogin WITH PASSWORD = 'SQLPass1234!'
|
||||
GO
|
||||
|
||||
DROP USER IF EXISTS WebUser
|
||||
CREATE USER WebUser FROM LOGIN WebLogin
|
||||
GO
|
||||
|
||||
DROP ROLE IF EXISTS WebUserRole
|
||||
CREATE ROLE WebUserRole
|
||||
GO
|
||||
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE TO WebUserRole
|
||||
EXEC sp_addrolemember N'WebUserRole', N'WebUser'
|
||||
GO
|
||||
@@ -1,47 +1,14 @@
|
||||
--CREATE DATABASE TodoDb;
|
||||
--USE TodoDb;
|
||||
DROP TABLE IF EXISTS Todo
|
||||
DROP PROCEDURE IF EXISTS createTodo
|
||||
DROP PROCEDURE IF EXISTS updateTodo
|
||||
DROP TABLE IF EXISTS Comments
|
||||
GO
|
||||
|
||||
CREATE TABLE Todo (
|
||||
CREATE TABLE Comments (
|
||||
id int IDENTITY PRIMARY KEY,
|
||||
title nvarchar(30) NOT NULL,
|
||||
description nvarchar(4000),
|
||||
completed bit,
|
||||
dueDate datetime2 default (dateadd(day, 3, getdate()))
|
||||
author nvarchar(30) NOT NULL,
|
||||
text nvarchar(4000)
|
||||
)
|
||||
GO
|
||||
|
||||
INSERT INTO Todo (title, description, completed, dueDate)
|
||||
INSERT INTO Comments (author, text)
|
||||
VALUES
|
||||
('Install SQL Server 2016','Install RTM version of SQL Server 2016', 0, '2017-03-08'),
|
||||
('Get new samples','Go to github and download new samples', 0, '2016-03-09'),
|
||||
('Try new samples','Install new Management Studio to try samples', 0, '2016-03-12')
|
||||
|
||||
GO
|
||||
|
||||
create procedure dbo.createTodo(@todo nvarchar(max))
|
||||
as begin
|
||||
insert into Todo
|
||||
select *
|
||||
from OPENJSON(@todo)
|
||||
WITH ( title nvarchar(30), description nvarchar(4000),
|
||||
completed bit, dueDate datetime2)
|
||||
end
|
||||
GO
|
||||
|
||||
create procedure updateTodo(@id int, @todo nvarchar(max))
|
||||
as begin
|
||||
update Todo
|
||||
set title = json.title, description = json.description,
|
||||
completed = json.completed, dueDate = json.dueDate
|
||||
from OPENJSON( @todo )
|
||||
WITH( title nvarchar(30), description nvarchar(4000),
|
||||
completed bit, dueDate datetime2) AS json
|
||||
where id = @id
|
||||
end
|
||||
go
|
||||
|
||||
select * from todo for json path
|
||||
('John','This is great!'),
|
||||
('Jane','I like the fact that it is simple.')
|
||||
Reference in New Issue
Block a user