-
Notifications
You must be signed in to change notification settings - Fork 1
/
complaint_table.sql
255 lines (197 loc) · 6.22 KB
/
complaint_table.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
Deployment script for Complaint_db
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 "Complaint_db"
:setvar DefaultFilePrefix "Complaint_db"
:setvar DefaultDataPath "C:\Users\pratik\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\ProjectsV13\"
:setvar DefaultLogPath "C:\Users\pratik\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\ProjectsV13\"
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].[complaints]...';
GO
CREATE TABLE [dbo].[complaints] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[title] VARCHAR (50) NULL,
[department] INT NOT NULL,
[description] VARCHAR (255) NULL,
[address] VARCHAR (255) NULL,
[city] VARCHAR (50) NULL,
[pincode] INT NULL,
[photo] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([Id] 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 Table [dbo].[departments]...';
GO
CREATE TABLE [dbo].[departments] (
[Id] INT NOT NULL,
[department_name] VARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([Id] 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 Foreign Key unnamed constraint on [dbo].[complaints]...';
GO
ALTER TABLE [dbo].[complaints] WITH NOCHECK
ADD FOREIGN KEY ([department]) REFERENCES [dbo].[departments] ([Id]);
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'Checking existing data against newly created constraints';
GO
USE [$(DatabaseName)];
GO
CREATE TABLE [#__checkStatus] (
id INT IDENTITY (1, 1) PRIMARY KEY CLUSTERED,
[Schema] NVARCHAR (256),
[Table] NVARCHAR (256),
[Constraint] NVARCHAR (256)
);
SET NOCOUNT ON;
DECLARE tableconstraintnames CURSOR LOCAL FORWARD_ONLY
FOR SELECT SCHEMA_NAME([schema_id]),
OBJECT_NAME([parent_object_id]),
[name],
0
FROM [sys].[objects]
WHERE [parent_object_id] IN (OBJECT_ID(N'dbo.complaints'))
AND [type] IN (N'F', N'C')
AND [object_id] IN (SELECT [object_id]
FROM [sys].[check_constraints]
WHERE [is_not_trusted] <> 0
AND [is_disabled] = 0
UNION
SELECT [object_id]
FROM [sys].[foreign_keys]
WHERE [is_not_trusted] <> 0
AND [is_disabled] = 0);
DECLARE @schemaname AS NVARCHAR (256);
DECLARE @tablename AS NVARCHAR (256);
DECLARE @checkname AS NVARCHAR (256);
DECLARE @is_not_trusted AS INT;
DECLARE @statement AS NVARCHAR (1024);
BEGIN TRY
OPEN tableconstraintnames;
FETCH tableconstraintnames INTO @schemaname, @tablename, @checkname, @is_not_trusted;
WHILE @@fetch_status = 0
BEGIN
PRINT N'Checking constraint: ' + @checkname + N' [' + @schemaname + N'].[' + @tablename + N']';
SET @statement = N'ALTER TABLE [' + @schemaname + N'].[' + @tablename + N'] WITH ' + CASE @is_not_trusted WHEN 0 THEN N'CHECK' ELSE N'NOCHECK' END + N' CHECK CONSTRAINT [' + @checkname + N']';
BEGIN TRY
EXECUTE [sp_executesql] @statement;
END TRY
BEGIN CATCH
INSERT [#__checkStatus] ([Schema], [Table], [Constraint])
VALUES (@schemaname, @tablename, @checkname);
END CATCH
FETCH tableconstraintnames INTO @schemaname, @tablename, @checkname, @is_not_trusted;
END
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE();
END CATCH
IF CURSOR_STATUS(N'LOCAL', N'tableconstraintnames') >= 0
CLOSE tableconstraintnames;
IF CURSOR_STATUS(N'LOCAL', N'tableconstraintnames') = -1
DEALLOCATE tableconstraintnames;
SELECT N'Constraint verification failed:' + [Schema] + N'.' + [Table] + N',' + [Constraint]
FROM [#__checkStatus];
IF @@ROWCOUNT > 0
BEGIN
DROP TABLE [#__checkStatus];
RAISERROR (N'An error occurred while verifying constraints', 16, 127);
END
SET NOCOUNT OFF;
DROP TABLE [#__checkStatus];
GO
PRINT N'Update complete.';
GO