1
+ DECLARE @KernelTables TABLE (
2
+ TableName NVARCHAR (200 ),
3
+ TableNumber Int );
4
+
5
+ DECLARE @ResultKernelTables TABLE (
6
+ TableNumber Int );
7
+
8
+ -- List of all Kernel Tables, with a unique TableNumber
9
+ INSERT INTO @KernelTables(TableName, TableNumber) VALUES (' SQLDICTIONARY' ,1 )
10
+ INSERT INTO @KernelTables(TableName, TableNumber) VALUES (' SYSCONFIG' ,2 )
11
+ INSERT INTO @KernelTables(TableName, TableNumber) VALUES (' USERINFO' ,3 )
12
+ INSERT INTO @KernelTables(TableName, TableNumber) VALUES (' SECURITYROLE' ,4 )
13
+ INSERT INTO @KernelTables(TableName, TableNumber) VALUES (' DATABASELOG' ,5 )
14
+ INSERT INTO @KernelTables(TableName, TableNumber) VALUES (' AOSDUPLICATEKEYEXCEPTIONMESSAGE' ,6 )
15
+ INSERT INTO @KernelTables(TableName, TableNumber) VALUES (' TIMEZONESLIST' ,7 )
16
+ INSERT INTO @KernelTables(TableName, TableNumber) VALUES (' TIMEZONESRULESDATA' ,8 )
17
+
18
+ -- get the KernelTable names
19
+ DECLARE KernelTableName_cursor CURSOR LOCAL FOR
20
+ SELECT TableName, TableNumber
21
+ FROM @KernelTables
22
+
23
+ -- (-1) : Exception happened
24
+ -- 0 : Dropped no column
25
+ -- 1 : Dropped atleast one Kernel Table column
26
+
27
+ DECLARE @Result INT = 0 ;
28
+ DECLARE @KernelTableName NVARCHAR (200 );
29
+ DECLARE @KernelTableNumber INT ;
30
+ DECLARE @SqlCmd NVARCHAR (500 );
31
+
32
+ BEGIN TRY
33
+ BEGIN TRANSACTION T1
34
+
35
+ OPEN KernelTableName_cursor;
36
+
37
+ FETCH NEXT FROM KernelTableName_cursor INTO @KernelTableName, @KernelTableNumber;
38
+
39
+ WHILE @@FETCH_STATUS = 0
40
+ BEGIN
41
+
42
+ IF COL_LENGTH (@KernelTableName, ' SYSROWVERSIONNUMBER' ) IS NOT NULL
43
+ BEGIN
44
+ SET @SqlCmd = ' ALTER TABLE dbo.' + @KernelTableName + ' DROP COLUMN SYSROWVERSIONNUMBER' ;
45
+ EXEC sp_executesql @SqlCmd;
46
+ SET @Result = 1 ;
47
+ INSERT INTO @ResultKernelTables(TableNumber) VALUES (@KernelTableNumber);
48
+ END
49
+
50
+ FETCH NEXT FROM KernelTableName_cursor INTO @KernelTableName, @KernelTableNumber;
51
+
52
+ END
53
+
54
+
55
+ COMMIT TRANSACTION T1
56
+
57
+ SELECT @Result AS Result, TableNumber AS KernelTableNumber, 0 AS Error, ' ' AS ErrorMessage
58
+ FROM @ResultKernelTables;
59
+
60
+ END TRY
61
+
62
+ BEGIN CATCH
63
+ SELECT - 1 AS Result, - 1 AS KernelTableNumber, ERROR_NUMBER () as Error, ERROR_MESSAGE () as ErrorMessage
64
+ ROLLBACK TRANSACTION T1
65
+ END CATCH
0 commit comments