|
| 1 | +DROP TABLE IF EXISTS Todo |
| 2 | +DROP PROCEDURE IF EXISTS createTodo |
| 3 | +DROP PROCEDURE IF EXISTS updateTodo |
| 4 | +GO |
| 5 | + |
| 6 | +CREATE TABLE Todo ( |
| 7 | + Id int IDENTITY PRIMARY KEY, |
| 8 | + Title nvarchar(30) NOT NULL, |
| 9 | + Description nvarchar(4000), |
| 10 | + Completed bit, |
| 11 | + TargetDate datetime2 |
| 12 | +) |
| 13 | +GO |
| 14 | + |
| 15 | +INSERT INTO Todo (Title, Description, Completed, TargetDate) |
| 16 | +VALUES |
| 17 | +('Install SQL Server 2016','Install RTM version of SQL Server 2016', 0, '2016-06-01'), |
| 18 | +('Get new samples','Go to github and download new samples', 0, '2016-06-02'), |
| 19 | +('Try new samples','Install new Management Studio to try samples', 0, '2016-06-02') |
| 20 | + |
| 21 | +GO |
| 22 | + |
| 23 | +create procedure dbo.createTodo(@todo nvarchar(max)) |
| 24 | +as begin |
| 25 | + insert into Todo |
| 26 | + select * |
| 27 | + from OPENJSON(@todo) |
| 28 | + WITH ( Title nvarchar(30), Description nvarchar(4000), |
| 29 | + Completed bit, TargetDate datetime2) |
| 30 | +end |
| 31 | +GO |
| 32 | + |
| 33 | +create procedure updateTodo(@id int, @todo nvarchar(max)) |
| 34 | +as begin |
| 35 | + update Todo |
| 36 | + set Title = json.Title, Description = json.Description, |
| 37 | + Completed = json.Completed, TargetDate = json.TargetDate |
| 38 | + from OPENJSON( @todo ) |
| 39 | + WITH( Title nvarchar(30), Description nvarchar(4000), |
| 40 | + Completed bit, TargetDate datetime2) AS json |
| 41 | + where Id = @id |
| 42 | +end |
0 commit comments