-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathSSDB.Row_Level_Auditing_Remove.sql
62 lines (50 loc) · 2.59 KB
/
SSDB.Row_Level_Auditing_Remove.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
CREATE PROCEDURE [admin].[RowLevelAuditingRemove]
@SchemaName NVARCHAR(50) = NULL
, @TableName NVARCHAR(50) = NULL
AS
BEGIN
/*
'--------------------------------------------------------------------------------------------------------------------
' Purpose: Removes row level auditing columns to a table
' Example: EXEC admin.RowLevelAuditingRemove 'dbo', 'Users';
'--------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------
-->>>>>>>>>>>>>>>>> FOR DEBUGGING <<<<<<<<<<<<<<<<<<<
-----------------------------------------------------
BEGIN
DECLARE @SchemaName NVARCHAR(50)
DECLARE @TableName NVARCHAR(50)
SET @SchemaName = 'dbo'
SET @TableName = 'Users'
-----------------------------------------------------
-----------------------------------------------------
*/
SET XACT_ABORT ON
BEGIN TRANSACTION;
SET NOCOUNT ON
DECLARE @SqlCommand NVARCHAR(1000)
DECLARE @CreatedId NVARCHAR(50)
DECLARE @CreatedDate NVARCHAR(50)
DECLARE @ModifiedId NVARCHAR(50)
DECLARE @ModifiedDate NVARCHAR(50)
SET @CreatedId = 'CreatedId'
SET @CreatedDate = 'CreatedDate'
SET @ModifiedId = 'ModifiedId'
SET @ModifiedDate = 'ModifiedDate'
SET @SqlCommand = 'DROP TRIGGER [' + @SchemaName + '].[TR_' + @TableName + '_LAST_UPDATED];' + CHAR(13);
SET @SqlCommand += 'ALTER TABLE ' + @SchemaName + '.' + @TableName + ' DROP CONSTRAINT [DF_' + @TableName + '_' + @CreatedId + '];' + CHAR(13);
SET @SqlCommand += 'ALTER TABLE ' + @SchemaName + '.' + @TableName + ' DROP CONSTRAINT [DF_' + @TableName + '_' + @CreatedDate + '];' + CHAR(13);
SET @SqlCommand += 'ALTER TABLE ' + @SchemaName + '.' + @TableName + ' DROP CONSTRAINT [DF_' + @TableName + '_' + @ModifiedId + '];' + CHAR(13);
SET @SqlCommand += 'ALTER TABLE ' + @SchemaName + '.' + @TableName + ' DROP CONSTRAINT [DF_' + @TableName + '_' + @ModifiedDate + '];' + CHAR(13);
SET @SqlCommand += 'ALTER TABLE ' + @SchemaName + '.' + @TableName + ' DROP COLUMN ' + @CreatedId + ';' + CHAR(13);
SET @SqlCommand += 'ALTER TABLE ' + @SchemaName + '.' + @TableName + ' DROP COLUMN ' + @CreatedDate + ';' + CHAR(13);
SET @SqlCommand += 'ALTER TABLE ' + @SchemaName + '.' + @TableName + ' DROP COLUMN ' + @ModifiedId + ';' + CHAR(13);
SET @SqlCommand += 'ALTER TABLE ' + @SchemaName + '.' + @TableName + ' DROP COLUMN ' + @ModifiedDate + ';' + CHAR(13);
PRINT @SqlCommand
EXEC (@SqlCommand)
--PRINT 'ROLLBACK TRANSACTION... ';
--ROLLBACK TRANSACTION;
PRINT 'COMMIT TRANSACTION... '
COMMIT TRANSACTION;
END
GO