forked from extnet/Ext.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleTasks_Reset.sql
More file actions
176 lines (121 loc) · 4.02 KB
/
SimpleTasks_Reset.sql
File metadata and controls
176 lines (121 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use "SimpleTasks"
go
/* DROP OBJECTS */
if exists (select * from sys.objects where name = 'Reset')
drop procedure "Reset"
GO
CREATE PROCEDURE Reset
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
BEGIN
EXECUTE sp_executesql N'/* INITIALIZE */
SET DATEFORMAT ymd
use "SimpleTasks"
/* DROP OBJECTS */
if exists (select * from sys.objects where name = ''Task'')
drop table "Task"
if exists (select * from sys.objects where name = ''Category'')
drop table "Category"
if exists (select * from sys.objects where name = ''IsListParent'')
drop function "IsListParent"
/* CREATE TABLES */
CREATE TABLE "Category" (
"ID" "int" IDENTITY (1, 1) NOT NULL ,
"ParentID" "int" NULL,
"Name" nvarchar (MAX) NOT NULL ,
"IsFolder" bit NOT NULL,
CONSTRAINT "PK_Category" PRIMARY KEY CLUSTERED
(
"ID"
),
CONSTRAINT "FK_Category_Category" FOREIGN KEY
(
"ParentID"
) REFERENCES "Category" (
"ID"
)
)
CREATE TABLE "Task" (
"ID" "int" IDENTITY (1, 1) NOT NULL ,
"CategoryID" "int" NOT NULL,
"Title" nvarchar (MAX) NOT NULL ,
"Description" nvarchar (MAX) NULL ,
"DueDate" "datetime" NOT NULL ,
"CompletedDate" "datetime" NULL ,
"Reminder" "datetime" NULL,
"Completed" bit NOT NULL,
CONSTRAINT "PK_Task" PRIMARY KEY CLUSTERED
(
"ID"
),
CONSTRAINT "FK_Task_Category" FOREIGN KEY
(
"CategoryID"
) REFERENCES "Category" (
"ID"
)
)
/* ADD DATA */
set quoted_identifier on
set identity_insert "Category" on
ALTER TABLE "Category" NOCHECK CONSTRAINT ALL
INSERT "Category"("ID", "ParentID", "Name", "IsFolder") VALUES(1, null, ''All Lists'', 1)
INSERT "Category"("ID", "ParentID", "Name", "IsFolder") VALUES(2, 1, ''Work'', 1)
INSERT "Category"("ID", "ParentID", "Name", "IsFolder") VALUES(3, 1, ''Home'', 1)
INSERT "Category"("ID", "ParentID", "Name", "IsFolder") VALUES(4, 1, ''Misc'', 1)
set identity_insert "Category" off
ALTER TABLE "Category" CHECK CONSTRAINT ALL
set identity_insert "Task" on
ALTER TABLE "Task" NOCHECK CONSTRAINT ALL
DECLARE @Date DATETIME = GETDATE()
Declare @DayOfMonth TinyInt Set @DayOfMonth = DAY(@Date) - 1
Declare @Month TinyInt Set @Month = MONTH(@Date) - 1
Declare @Year Integer Set @Year = YEAR(@Date)
SET @Date = DateAdd(day, @DayOfMonth,
DateAdd(month, @Month,
DateAdd(Year, @Year-1900, 0)))
/* SUBTRACT 3 DAYS */
INSERT "Task"("ID", "CategoryID", "Title", "Description", "DueDate", "CompletedDate", "Reminder", "Completed")
VALUES(1, 4, ''Groceries'', ''Milk and Eggs'', DATEADD(DAY, -3, @Date), DATEADD(DAY, -3, @Date), null, 1)
/* SUBTRACT 2 DAYS */
INSERT "Task"("ID", "CategoryID", "Title", "Description", "DueDate", "CompletedDate", "Reminder", "Completed")
VALUES(2, 3, ''Bike Tune-up'', ''Drop off bike for tune-up'', DATEADD(DAY, -2, @Date), DATEADD(DAY, -2, @Date), null, 1)
/* TODAY */
INSERT "Task"("ID", "CategoryID", "Title", "Description", "DueDate", "CompletedDate", "Reminder", "Completed")
VALUES(3, 2, ''Finish demo application'', null, @Date, null, null, 0)
INSERT "Task"("ID", "CategoryID", "Title", "Description", "DueDate", "CompletedDate", "Reminder", "Completed")
VALUES(4, 3, ''Movie Night'', ''Rent Family Movie'', @Date, null, null, 0)
/* PLUS 20 DAYS */
INSERT "Task"("ID", "CategoryID", "Title", "Description", "DueDate", "CompletedDate", "Reminder", "Completed")
VALUES(5, 2, ''Documentation Kick Off'', ''Meeting to discuss documentation project'', DATEADD(DAY, 20, @Date), null, null, 0)
set identity_insert "Task" off
ALTER TABLE "Task" CHECK CONSTRAINT ALL
'
EXECUTE sp_executesql N'
CREATE FUNCTION IsListParent
(
@targetNode int,
@dropNode int
)
RETURNS bit
AS
BEGIN
WHILE (EXISTS(SELECT * FROM Category WHERE ParentID = @dropNode ))
BEGIN
SELECT @dropNode = ID FROM Category WHERE ParentID = @dropNode
IF @dropNode = @targetNode
BEGIN
RETURN 1
END
END
RETURN 0
END
'
RETURN
END
GO