-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSQLQuery.sql
227 lines (172 loc) · 4.43 KB
/
SQLQuery.sql
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Deployment script for E:\REPOS\EMPLOYEEMANAGEMENTSYSTEM\DATABASE.MDF
This code was generated by a tool.
Changes to this file may cause incorrect behavior and will be lost if
the code is regenerated.
*/
/
GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
GO
:setvar DatabaseName "E:\REPOS\EMPLOYEEMANAGEMENTSYSTEM\DATABASE.MDF"
:setvar DefaultFilePrefix "E_\REPOS\EMPLOYEEMANAGEMENTSYSTEM\DATABASE.MDF_"
:setvar DefaultDataPath "C:\Users\byusu\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\mssqllocaldb\"
:setvar DefaultLogPath "C:\Users\byusu\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\mssqllocaldb\"
GO
:on error exit
GO
/*
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
To re-enable the script after enabling SQLCMD mode, execute the following:
SET NOEXEC OFF;
*/
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
BEGIN
PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
SET NOEXEC ON;
END
GO
USE [$(DatabaseName)];
GO
IF (SELECT OBJECT_ID('tempdb..#tmpErrors')) IS NOT NULL DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
GO
BEGIN TRANSACTION
GO
PRINT N'Creating Table [dbo].[Table]...';
GO
CREATE TABLE [dbo].[Table] (
[Id] INT NOT NULL,
[Username] NCHAR (50) NOT NULL,
[Password] NCHAR (100) NOT NULL,
[Name] NCHAR (50) NOT NULL,
[HourlyRate] MONEY NOT NULL,
[CompletedTasks] INT NOT NULL,
[UncompletedTasks] INT NOT NULL,
[IsEmployee] BIT NOT NULL,
[IsAdmin] BIT NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
UNIQUE NONCLUSTERED ([Username] ASC)
);
GO
IF @@ERROR <> 0
AND @@TRANCOUNT > 0
BEGIN
ROLLBACK;
END
IF OBJECT_ID(N'tempdb..#tmpErrors') IS NULL
CREATE TABLE [#tmpErrors] (
Error INT
);
IF @@TRANCOUNT = 0
BEGIN
INSERT INTO #tmpErrors (Error)
VALUES (1);
BEGIN TRANSACTION;
END
GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[Table]...';
GO
ALTER TABLE [dbo].[Table]
ADD DEFAULT 0 FOR [CompletedTasks];
GO
IF @@ERROR <> 0
AND @@TRANCOUNT > 0
BEGIN
ROLLBACK;
END
IF OBJECT_ID(N'tempdb..#tmpErrors') IS NULL
CREATE TABLE [#tmpErrors] (
Error INT
);
IF @@TRANCOUNT = 0
BEGIN
INSERT INTO #tmpErrors (Error)
VALUES (1);
BEGIN TRANSACTION;
END
GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[Table]...';
GO
ALTER TABLE [dbo].[Table]
ADD DEFAULT 0 FOR [UncompletedTasks];
GO
IF @@ERROR <> 0
AND @@TRANCOUNT > 0
BEGIN
ROLLBACK;
END
IF OBJECT_ID(N'tempdb..#tmpErrors') IS NULL
CREATE TABLE [#tmpErrors] (
Error INT
);
IF @@TRANCOUNT = 0
BEGIN
INSERT INTO #tmpErrors (Error)
VALUES (1);
BEGIN TRANSACTION;
END
GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[Table]...';
GO
ALTER TABLE [dbo].[Table]
ADD DEFAULT 0 FOR [IsEmployee];
GO
IF @@ERROR <> 0
AND @@TRANCOUNT > 0
BEGIN
ROLLBACK;
END
IF OBJECT_ID(N'tempdb..#tmpErrors') IS NULL
CREATE TABLE [#tmpErrors] (
Error INT
);
IF @@TRANCOUNT = 0
BEGIN
INSERT INTO #tmpErrors (Error)
VALUES (1);
BEGIN TRANSACTION;
END
GO
PRINT N'Creating Default Constraint unnamed constraint on [dbo].[Table]...';
GO
ALTER TABLE [dbo].[Table]
ADD DEFAULT 0 FOR [IsAdmin];
GO
IF @@ERROR <> 0
AND @@TRANCOUNT > 0
BEGIN
ROLLBACK;
END
IF OBJECT_ID(N'tempdb..#tmpErrors') IS NULL
CREATE TABLE [#tmpErrors] (
Error INT
);
IF @@TRANCOUNT = 0
BEGIN
INSERT INTO #tmpErrors (Error)
VALUES (1);
BEGIN TRANSACTION;
END
GO
IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT N'The transacted portion of the database update succeeded.'
COMMIT TRANSACTION
END
ELSE PRINT N'The transacted portion of the database update failed.'
GO
IF (SELECT OBJECT_ID('tempdb..#tmpErrors')) IS NOT NULL DROP TABLE #tmpErrors
GO
GO
PRINT N'Update complete.';
GO