-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.sql
More file actions
558 lines (502 loc) · 29.1 KB
/
Copy pathgenerator.sql
File metadata and controls
558 lines (502 loc) · 29.1 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**************************************************************************************************
* SQL Server Stored Procedure Generator
* Author : Hossam Ismail (Neolorn)
* Version : 5.2.0
* License : MIT
* Repository : https://github.com/neolorn/SQLGenerator
*
* Overview:
* This script dynamically generates standardized CRUD stored procedures for user-defined tables
* in Microsoft SQL Server. It's optimized for scaffolding, rapid prototyping, and automation.
*
* Generated Procedures per Table:
* - <Table>_Select : Supports key-set and offset pagination with optional full-text search.
* - <Table>_SelectById : Fetches a single record by primary key(s).
* - <Table>_Insert : Inserts a new row with support for NULL/defaults and ID output.
* - <Table>_Update : Updates columns using COALESCE semantics for patch-style behavior.
* - <Table>_Delete : Deletes a row by primary key(s).
*
* Compatibility (= tested on):
* - SQL Server 2019 and above
*
* Use Cases:
* - Rapid prototyping
* - Internal tooling and automation
* - Schema-first development
*
* Change Log:
* v5.2.0 - Added conditional key-set/offset pagination + single-key optimization
* v5.1.2 - Added multi-table support, computed column search, fixed GUID insert handling
* v5.0.0 - Rewriten for GitHub release
*/
/**************************************************************************************************
-- START CONFIGURATION SETTINGS
**************************************************************************************************/
-- Formatting constants
DECLARE @S VARCHAR(1) = CHAR(32); -- Single space
DECLARE @L VARCHAR(1) = CHAR(13); -- Line break
DECLARE @T VARCHAR(1) = CHAR(9); -- Tab
DECLARE @TT VARCHAR(2) = @T + CHAR(9);
DECLARE @TTT VARCHAR(3) = @TT + CHAR(9);
-- Database and table configuration
DECLARE @TargetTables VARCHAR(MAX); -- Comma-separated list; leave blank for all tables
SET @TargetTables = '';
DECLARE @TargetSchema VARCHAR(MAX);
SET @TargetSchema = 'dbo';
DECLARE @TargetDatabase VARCHAR(MAX);
SET @TargetDatabase = '';
DECLARE @OutputMethod VARCHAR(MAX); -- Options: 'Print' or 'Execute'
SET @OutputMethod = 'Print';
DECLARE @ExcludePrefix VARCHAR(MAX); -- Optional prefix to strip from table names
SET @ExcludePrefix = '';
DECLARE @UseSelectWildCard BIT; -- Set to 1 to generate SELECT * instead of listing each column
SET @UseSelectWildCard = 0;
DECLARE @SearchColumn VARCHAR(MAX); -- Computed column for generic search
SET @SearchColumn = 'Summary';
/**************************************************************************************************
-- END CONFIGURATION SETTINGS
**************************************************************************************************/
-- Flag indicating if the computed search column exists in the table
DECLARE @CanSearch BIT;
-- Variable to accumulate column list for SELECT (when not using wildcard)
DECLARE @ColumnList NVARCHAR(MAX) = '';
-- Cursor to iterate over all user table columns
DECLARE ColumnCursor CURSOR FOR
SELECT c.TABLE_SCHEMA,
c.TABLE_NAME,
c.COLUMN_NAME,
c.DATA_TYPE,
c.CHARACTER_MAXIMUM_LENGTH,
CASE
WHEN COLUMNPROPERTY(OBJECT_ID(QUOTENAME(c.TABLE_SCHEMA) + '.' + QUOTENAME(c.TABLE_NAME)),
c.COLUMN_NAME, 'IsComputed') = 1 THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT)
END AS IsComputed
FROM INFORMATION_SCHEMA.COLUMNS c
INNER JOIN INFORMATION_SCHEMA.TABLES t
ON c.TABLE_NAME = t.TABLE_NAME AND c.TABLE_SCHEMA = t.TABLE_SCHEMA
WHERE t.TABLE_CATALOG = @TargetDatabase
AND t.TABLE_TYPE = 'BASE TABLE'
AND t.TABLE_SCHEMA = @TargetSchema
ORDER BY c.TABLE_NAME, c.ORDINAL_POSITION;
-- Column-level variables
DECLARE @TableSchema VARCHAR(MAX);
DECLARE @TableName VARCHAR(MAX);
DECLARE @ColumnName VARCHAR(MAX);
DECLARE @DataType VARCHAR(MAX);
DECLARE @CharLength INT;
DECLARE @IsComputed BIT;
DECLARE @IsPrimaryKey BIT;
DECLARE @IdColumnExists BIT;
-- Table-level trackers
DECLARE @CurrentTable VARCHAR(MAX);
DECLARE @PrimaryKeyColumn VARCHAR(MAX);
DECLARE @PrimaryKeyDataType VARCHAR(MAX);
DECLARE @RawTableName VARCHAR(MAX);
DECLARE @ExcludePrefixLength INT;
-- Compound primary key trackers
DECLARE @PKParameters NVARCHAR(MAX) = ''; -- Pure comma-separated list of key parameters (each on its own line)
DECLARE @PKWhereClause NVARCHAR(MAX) = '';
-- New variables for key-set logic over all PKs
DECLARE @PKCondition NVARCHAR(MAX) = '';
DECLARE @PKCaseColumn NVARCHAR(MAX) = '';
DECLARE @PKCaseParam NVARCHAR(MAX) = '';
DECLARE @PKCount INT;
-- Stored procedure definition builders
DECLARE @SelectProcedure NVARCHAR(MAX);
DECLARE @SelectByIdProcedure NVARCHAR(MAX);
DECLARE @InsertIntoClause NVARCHAR(MAX);
DECLARE @InsertValuesClause NVARCHAR(MAX);
DECLARE @InsertProcedure NVARCHAR(MAX);
DECLARE @UpdateSetClause NVARCHAR(MAX);
DECLARE @UpdateProcedure NVARCHAR(MAX);
DECLARE @DeleteProcedure NVARCHAR(MAX);
DECLARE @FinalOutput NVARCHAR(MAX) = N'';
-- Initialize trackers
SET @CurrentTable = '';
SET @ExcludePrefixLength = LEN(@ExcludePrefix);
OPEN ColumnCursor;
FETCH NEXT FROM ColumnCursor INTO @TableSchema, @TableName, @ColumnName, @DataType, @CharLength, @IsComputed;
SET @DataType = UPPER(@DataType);
-- Determine if the current column is part of the primary key.
SET @IsPrimaryKey =
CASE
WHEN EXISTS (SELECT 1
FROM sys.indexes i
INNER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.columns c2 ON ic.object_id = c2.object_id AND ic.column_id = c2.column_id
WHERE i.object_id = OBJECT_ID(QUOTENAME(@TableSchema) + '.' + QUOTENAME(@TableName))
AND i.is_primary_key = 1
AND c2.name = @ColumnName) THEN 1
ELSE 0
END;
-- Loop through each column for all tables
WHILE @@FETCH_STATUS = 0
BEGIN
-- If this column is the computed search column, mark search support as available.
IF UPPER(@ColumnName) = UPPER(@SearchColumn) AND @IsComputed = 1
SET @CanSearch = 1;
-- When a new table is encountered...
IF @TableName <> @CurrentTable
BEGIN
-- Finalize stored procedures for the previous table (if any)
IF @CurrentTable <> ''
BEGIN
IF @TargetTables = '' OR ',' + @TargetTables + ',' LIKE '%,' + @CurrentTable + ',%'
BEGIN
-- Finalize the SELECT procedure with the new pagination logic
-- Use @PageNumber branch with OFFSET (ORDER BY first PK column)
SET @SelectProcedure = @SelectProcedure +
@T + 'IF @PageNumber IS NOT NULL' + @L +
@T + 'BEGIN' + @L +
@TT + 'SELECT' + @S + @ColumnList + @L +
@TT + 'FROM' + @S + @TableSchema + '.' + @CurrentTable + @S + 'WITH(NOLOCK)' +
@L +
(CASE
WHEN @CanSearch = 1 THEN @TT + 'WHERE (@SearchTerm IS NULL OR' + @S +
@SearchColumn + @S +
'LIKE ''%'' + @SearchTerm + ''%'')' + @L
ELSE ''
END) +
@TT + 'ORDER BY' + @S + @PrimaryKeyColumn + @L +
@TT + 'OFFSET ((@PageNumber - 1) * @PageSize) ROWS' + @L +
@TT + 'FETCH NEXT @PageSize ROWS ONLY;' + @L +
@T + 'END' + @L;
-- Use key-set pagination if any PK parameter is non-null (using the CASE expressions)
SET @SelectProcedure = @SelectProcedure +
@T + 'ELSE IF (' + @PKCondition + ')' + @L +
@T + 'BEGIN' + @L +
@TT + 'SELECT TOP (@PageSize)' + @S + @ColumnList + @L +
@TT + 'FROM' + @S + @TableSchema + '.' + @CurrentTable + @S + 'WITH(NOLOCK)' +
@L +
@TT + 'WHERE (' + @PKCaseColumn + ') > (' + @PKCaseParam + ')' + @S +
(CASE
WHEN @CanSearch = 1 THEN 'AND (@SearchTerm IS NULL OR ' + @SearchColumn + @S +
'LIKE ''%'' + @SearchTerm + ''%'')'
ELSE ''
END) + @L +
@TT + 'ORDER BY (' + @PKCaseColumn + ');' + @L +
@T + 'END' + @L;
-- Else, if neither page nor PK is supplied, return the entire table.
SET @SelectProcedure = @SelectProcedure +
@T + 'ELSE' + @L +
@T + 'BEGIN' + @L +
@TT + 'SELECT' + @S + @ColumnList + @L +
@TT + 'FROM' + @S + @TableSchema + '.' + @CurrentTable + @S + 'WITH(NOLOCK)' +
@L +
(CASE
WHEN @CanSearch = 1 THEN @TT + 'WHERE (@SearchTerm IS NULL OR' + @S +
@SearchColumn + @S +
'LIKE ''%'' + @SearchTerm + ''%'')' + @L
ELSE ''
END) +
@T + 'END' + @L +
@T + 'SET NOCOUNT OFF' + @L +
'END' + @L +
(CASE WHEN @OutputMethod <> 'Execute' THEN 'GO' ELSE '' END) + @L + @L;
-- Finalize other procedures (unchanged parts) ...
SET @SelectByIdProcedure = @SelectByIdProcedure + @L +
@T + 'FROM' + @S + @TableSchema + '.' + @CurrentTable + @S + 'WITH(NOLOCK)' +
@L +
@T + 'WHERE' + @S + @PKWhereClause + @L +
@T + 'SET NOCOUNT OFF' + @L +
'END' + @L +
(CASE WHEN @OutputMethod <> 'Execute' THEN 'GO' ELSE '' END) + @L + @L;
SET @InsertIntoClause =
SUBSTRING(@InsertIntoClause, 0, LEN(@InsertIntoClause) - 1) + @L + @T + ')' + @L;
SET @InsertValuesClause =
SUBSTRING(@InsertValuesClause, 0, LEN(@InsertValuesClause) - 1) + @L + @T + ')';
SET @InsertProcedure = @InsertProcedure + @L +
'AS' + @L +
'BEGIN' + @L +
@T + 'SET NOCOUNT ON' + @L +
@InsertIntoClause +
CASE
WHEN @IdColumnExists = 1 THEN @T + 'OUTPUT INSERTED.Id' + @L
ELSE ''
END +
@InsertValuesClause + @L +
@T + 'SET NOCOUNT OFF' + @L +
'END' + @L +
(CASE WHEN @OutputMethod <> 'Execute' THEN 'GO' ELSE '' END) + @L + @L;
SET @UpdateSetClause = SUBSTRING(@UpdateSetClause, 0, LEN(@UpdateSetClause) - 1) + @L +
@T + 'WHERE' + @S + @PKWhereClause;
SET @UpdateProcedure = @UpdateProcedure + @L +
'AS' + @L +
'BEGIN' + @L +
@T + 'SET NOCOUNT ON' + @L +
@UpdateSetClause + @L +
@T + 'SET NOCOUNT OFF' + @L +
'END' + @L +
(CASE WHEN @OutputMethod <> 'Execute' THEN 'GO' ELSE '' END) + @L + @L;
IF @OutputMethod <> 'Execute'
SET @FinalOutput = ISNULL(@FinalOutput, '') +
ISNULL(@SelectProcedure, '') +
ISNULL(@SelectByIdProcedure, '') +
ISNULL(@InsertProcedure, '') +
ISNULL(@UpdateProcedure, '') +
ISNULL(@DeleteProcedure, '');
ELSE
BEGIN
EXEC sp_executesql @SelectProcedure;
EXEC sp_executesql @SelectByIdProcedure;
EXEC sp_executesql @InsertProcedure;
EXEC sp_executesql @UpdateProcedure;
EXEC sp_executesql @DeleteProcedure;
END
END
END
-- Begin processing for the new table.
SET @CurrentTable = @TableName;
SET @PKParameters = '';
SET @PKWhereClause = '';
SET @PKCondition = '';
SET @PKCaseColumn = '';
SET @PKCaseParam = '';
SET @ColumnList = '';
-- Reset for the new table
-- Check if the table contains an 'ID' column.
SELECT @IdColumnExists = CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = @TableSchema
AND TABLE_NAME = @TableName
AND COLUMN_NAME = 'Id';
-- Build the compound primary key parameter list.
SELECT @PKParameters = STUFF((SELECT ',' + @L + @T + '@' + c.name + @S + UPPER(typ.name) +
CASE
WHEN typ.name IN ('varchar', 'nvarchar', 'char', 'nchar')
THEN '(' + CAST(c.max_length AS VARCHAR(10)) + ')'
ELSE ''
END
FROM sys.indexes i
INNER JOIN sys.index_columns ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.columns c
ON ic.object_id = c.object_id AND ic.column_id = c.column_id
INNER JOIN sys.types typ ON c.user_type_id = typ.user_type_id
WHERE
i.object_id = OBJECT_ID(QUOTENAME(@TableSchema) + '.' + QUOTENAME(@TableName))
AND i.is_primary_key = 1
ORDER BY ic.key_ordinal
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '');
SELECT @PKCount = COUNT(*)
FROM sys.index_columns ic
INNER JOIN sys.indexes i
ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE i.is_primary_key = 1
AND i.object_id = OBJECT_ID(QUOTENAME(@TableSchema) + '.' + QUOTENAME(@TableName));
-- Build the compound primary key WHERE clause.
SELECT @PKWhereClause = STUFF((SELECT @S + 'AND' + @S + c.name + @S + '= @' + c.name
FROM sys.indexes i
INNER JOIN sys.index_columns ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.columns c
ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE i.object_id =
OBJECT_ID(QUOTENAME(@TableSchema) + '.' + QUOTENAME(@TableName))
AND i.is_primary_key = 1
ORDER BY ic.key_ordinal
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 5, '');
-- Build the PK condition (checks if any PK parameter is non-null).
SELECT @PKCondition = STUFF((SELECT @S + 'OR' + @S + '@' + c.name + @S + 'IS NOT NULL'
FROM sys.indexes i
INNER JOIN sys.index_columns ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.columns c
ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE
i.object_id = OBJECT_ID(QUOTENAME(@TableSchema) + '.' + QUOTENAME(@TableName))
AND i.is_primary_key = 1
ORDER BY ic.key_ordinal
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 4, '');
IF @PKCount = 1
BEGIN
-- Use the single PK column directly
SET @PKCaseColumn = @PrimaryKeyColumn;
SET @PKCaseParam = '@' + @PrimaryKeyColumn;
END
ELSE
BEGIN
-- Build the PK CASE expressions for key-set pagination.
SELECT @PKCaseColumn = 'CASE' + @L +
STUFF((SELECT @L + @TTT + 'WHEN @' + c.name + @S + 'IS NOT NULL THEN' + @S + c.name
FROM sys.indexes i
INNER JOIN sys.index_columns ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.columns c
ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE i.object_id =
OBJECT_ID(QUOTENAME(@TableSchema) + '.' + QUOTENAME(@TableName))
AND i.is_primary_key = 1
ORDER BY ic.key_ordinal
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
+ @L + @TT + 'END';
SELECT @PKCaseParam = 'COALESCE(' +
STUFF((SELECT ', @' + c.name
FROM sys.indexes i
INNER JOIN sys.index_columns ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.columns c
ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE i.object_id =
OBJECT_ID(QUOTENAME(@TableSchema) + '.' + QUOTENAME(@TableName))
AND i.is_primary_key = 1
ORDER BY ic.key_ordinal
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '')
+ ')';
END
-- Retrieve details for the first primary key column (for OFFSET pagination ORDER BY).
SELECT TOP 1 @PrimaryKeyColumn = c.name,
@PrimaryKeyDataType = UPPER(typ.name),
@CharLength = c.max_length
FROM sys.indexes i
INNER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
INNER JOIN sys.types typ ON c.user_type_id = typ.user_type_id
WHERE i.object_id = OBJECT_ID(QUOTENAME(@TableSchema) + '.' + QUOTENAME(@TableName))
AND i.is_primary_key = 1
ORDER BY ic.key_ordinal;
-- Remove any defined prefix from the table name.
IF @ExcludePrefixLength > 0 AND SUBSTRING(@TableName, 0, @ExcludePrefixLength) = @ExcludePrefix
SET @RawTableName = RIGHT(@TableName, LEN(@TableName) - @ExcludePrefixLength);
ELSE
SET @RawTableName = @TableName;
-- Check for the computed search column in the current table.
SET @CanSearch = (SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = @TableSchema
AND TABLE_NAME = @TableName
AND UPPER(COLUMN_NAME) = UPPER(@SearchColumn)
AND COLUMNPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)),
COLUMN_NAME, 'IsComputed') = 1);
IF @TargetTables = '' OR ',' + @TargetTables + ',' LIKE '%,' + @TableName + ',%'
BEGIN
-- Build the header for the SELECT procedure using the new PK parameters and adding @PageNumber.
SET @SelectProcedure = 'CREATE PROCEDURE' + @S + @TargetSchema + '.' + @RawTableName + '_Select' +
'(' + @L +
@PKParameters + ',' + @L +
@T + '@PageSize INT = 10,' + @L +
@T + '@PageNumber INT = NULL';
IF @CanSearch = 1
SET @SelectProcedure = @SelectProcedure + ',' + @L +
@T + '@SearchTerm NVARCHAR(MAX) = NULL';
SET @SelectProcedure = @SelectProcedure + @L + ')' + @L +
'AS' + @L +
'BEGIN' + @L +
@T + 'SET NOCOUNT ON' + @L;
-- Initialize the SELECT column list.
IF @UseSelectWildCard = 1
SET @ColumnList = '*';
ELSE
BEGIN
-- For the first encountered column, set @ColumnList to its name.
SET @ColumnList = @L + @TTT + @ColumnName;
END;
-- Build the header for the SELECT BY ID procedure.
SET @SelectByIdProcedure = 'CREATE PROCEDURE' + @S + @TargetSchema + '.' + @RawTableName + '_SelectById' +
@L + @PKParameters + @L +
'AS' + @L +
'BEGIN' + @L +
@T + 'SET NOCOUNT ON' + @L;
IF @UseSelectWildCard = 1
SET @SelectByIdProcedure = @SelectByIdProcedure + @T + 'SELECT * ';
ELSE
SET @SelectByIdProcedure = @SelectByIdProcedure + @T + 'SELECT' + @S + @L +
@TT + @ColumnName;
-- Build the initial header for the INSERT procedure (only for non-computed columns).
IF @IsComputed = 0
BEGIN
SET @InsertProcedure = 'CREATE PROCEDURE' + @S + @TargetSchema + '.' + @RawTableName + '_Insert' + @L +
@T + '@' + @ColumnName + @S + @DataType;
IF @DataType IN ('VARCHAR', 'NVARCHAR', 'CHAR', 'NCHAR')
SET @InsertProcedure = @InsertProcedure + '(' + CAST(@CharLength AS VARCHAR(MAX)) + ')';
SET @InsertProcedure = @InsertProcedure + @S + '= NULL';
SET @UpdateProcedure = 'CREATE PROCEDURE' + @S + @TargetSchema + '.' + @RawTableName + '_Update' +
@L + @PKParameters;
END
-- Begin building the UPDATE SET clause.
SET @UpdateSetClause = @T + 'UPDATE' + @S + @TableName + @S + 'SET' + @S + @L;
IF @IsComputed = 0 AND @IsPrimaryKey = 0
SET @UpdateSetClause = @UpdateSetClause +
@TT + @ColumnName + @S + '= COALESCE(@' + @ColumnName + ', ' + @ColumnName + '),' +
@L;
-- Build the INSERT INTO clause.
SET @InsertIntoClause = @T + 'INSERT INTO' + @S + @TableSchema + '.' + @TableName + @S + '(' + @L;
SET @InsertValuesClause = @T + 'VALUES (' + @L;
IF @IsComputed = 0
BEGIN
SET @InsertIntoClause = @InsertIntoClause + @TT + @ColumnName + ',' + @L;
SET @InsertValuesClause = @InsertValuesClause + @TT + '@' + @ColumnName + ',' + @L;
END
-- Build the DELETE procedure.
SET @DeleteProcedure = 'CREATE PROCEDURE' + @S + @TargetSchema + '.' + @RawTableName + '_Delete' + @L +
@PKParameters + @L +
'AS' + @L +
'BEGIN' + @L +
@T + 'SET NOCOUNT ON' + @L +
@T + 'DELETE FROM' + @S + @TargetSchema + '.' + @TableName + @L +
@T + 'WHERE' + @S + @PKWhereClause + @L +
@T + 'SET NOCOUNT OFF' + @L +
'END' + @L +
(CASE WHEN @OutputMethod <> 'Execute' THEN 'GO' ELSE '' END) + @L + @L;
END
END
ELSE
BEGIN
IF @TargetTables = '' OR ',' + @TargetTables + ',' LIKE '%,' + @CurrentTable + ',%'
BEGIN
-- Append additional columns for SELECT procedures (if not using SELECT *).
IF @UseSelectWildCard = 0
BEGIN
SET @SelectByIdProcedure = @SelectByIdProcedure + ',' + @L + @TT + @ColumnName;
-- Also append to @ColumnList
SET @ColumnList = @ColumnList + ', ' + @L + @TTT + @ColumnName;
END
-- Append parameters for the INSERT procedure.
IF @IsComputed = 0
BEGIN
SET @InsertProcedure = @InsertProcedure + ',' + @L + @T + '@' + @ColumnName + @S + @DataType;
IF @DataType IN ('VARCHAR', 'NVARCHAR', 'CHAR', 'NCHAR')
SET @InsertProcedure = @InsertProcedure + '(' + CAST(@CharLength AS VARCHAR(MAX)) + ')';
SET @InsertProcedure = @InsertProcedure + @S + '= NULL';
END
-- Append parameters for the UPDATE procedure.
IF @IsComputed = 0 AND @IsPrimaryKey = 0
BEGIN
SET @UpdateProcedure = @UpdateProcedure + ',' + @L + @T + '@' + @ColumnName + @S + @DataType;
IF @DataType IN ('VARCHAR', 'NVARCHAR', 'CHAR', 'NCHAR')
SET @UpdateProcedure = @UpdateProcedure + '(' + CAST(@CharLength AS VARCHAR(MAX)) + ')';
SET @UpdateProcedure = @UpdateProcedure + @S + '= NULL';
END
-- Append to the UPDATE SET clause.
IF @IsComputed = 0 AND @IsPrimaryKey = 0
SET @UpdateSetClause = @UpdateSetClause +
@TT + @ColumnName + @S + '= COALESCE(@' + @ColumnName + ', ' + @ColumnName + '),' +
@L;
-- Append columns to the INSERT clauses.
IF @IsComputed = 0
BEGIN
SET @InsertIntoClause = @InsertIntoClause + @TT + @ColumnName + ',' + @L;
SET @InsertValuesClause = @InsertValuesClause + @TT + '@' + @ColumnName + ',' + @L;
END
END
END
FETCH NEXT FROM ColumnCursor INTO @TableSchema, @TableName, @ColumnName, @DataType, @CharLength, @IsComputed;
SET @DataType = UPPER(@DataType);
SET @IsPrimaryKey =
CASE
WHEN EXISTS (SELECT 1
FROM sys.indexes i
INNER JOIN sys.index_columns ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.columns c2 ON ic.object_id = c2.object_id AND ic.column_id = c2.column_id
WHERE i.object_id = OBJECT_ID(QUOTENAME(@TableSchema) + '.' + QUOTENAME(@TableName))
AND i.is_primary_key = 1
AND c2.name = @ColumnName) THEN 1
ELSE 0
END;
END
CLOSE ColumnCursor;
DEALLOCATE ColumnCursor;
-- If not executing directly, output the generated stored procedure scripts.
IF @OutputMethod <> 'Execute' SELECT @FinalOutput AS Output;