syncro
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
USE Arizona;
|
||||
GO
|
||||
IF OBJECT_ID('upd.alignIndexes') IS NOT NULL
|
||||
DROP PROCEDURE [upd].alignIndexes;
|
||||
GO
|
||||
@@ -61,7 +59,7 @@ BEGIN
|
||||
DECLARE @tableName VARCHAR(100); /* used in cursor */
|
||||
DECLARE @indexFK INT; /* used in cursor */
|
||||
DECLARE @msg VARCHAR(MAX) = '';
|
||||
DECLARE @indexesToMaintains TABLE
|
||||
DECLARE @indexesToMaintains TABLE /* working representation of upd.IA_IndexesAlignementActions */
|
||||
(
|
||||
[IA_indexesAlignementActions] INT NULL, /* FK to table upd.IA_IndexesAlignementActions */
|
||||
[SchemaName] VARCHAR(100) NOT NULL, /* the schema the table is part of */
|
||||
@@ -104,26 +102,26 @@ BEGIN
|
||||
[IA_indexesAlignementActions],
|
||||
[SchemaName],
|
||||
[TableName],
|
||||
expectedIndexName_inp,
|
||||
columnsName_inp,
|
||||
includedColumns_inp,
|
||||
isClustered_inp,
|
||||
isUnique_inp,
|
||||
isToBeDeleted_inp,
|
||||
isToBeRecreated_inp,
|
||||
IA_index_options
|
||||
[expectedIndexName_inp],
|
||||
[columnsName_inp],
|
||||
[includedColumns_inp],
|
||||
[isClustered_inp],
|
||||
[isUnique_inp],
|
||||
[isToBeDeleted_inp],
|
||||
[isToBeRecreated_inp],
|
||||
[IA_index_options]
|
||||
)
|
||||
SELECT ia.[IA_indexesAlignementActions_id],
|
||||
ia.[IA_schema_name],
|
||||
ia.[IA_table_name],
|
||||
ia.[IA_expected_index_name],
|
||||
ia.[IA_columns_name],
|
||||
ia.[IA_included_columns],
|
||||
ia.[IA_is_clustered],
|
||||
ia.[IA_is_unique],
|
||||
ia.[IA_is_to_be_deleted],
|
||||
ia.[IA_is_to_be_recreated],
|
||||
ia.IA_index_options
|
||||
SELECT ia.[IA_indexesAlignementActionsID],
|
||||
ia.[IA_schemaName],
|
||||
ia.[IA_tableName],
|
||||
ia.[IA_expectedIndexName],
|
||||
ia.[IA_columnsName],
|
||||
ia.[IA_includedColumns],
|
||||
ia.[IA_isClustered],
|
||||
ia.[IA_isUnique],
|
||||
ia.[IA_isToBeDeleted],
|
||||
ia.[IA_isToBeRecreated],
|
||||
ia.[IA_indexOptions]
|
||||
FROM [upd].[IA_IndexesAlignementActions] ia
|
||||
WHERE ia.[IA_executionDate] IS NULL;
|
||||
|
||||
@@ -131,44 +129,44 @@ BEGIN
|
||||
/* logic checks on index definition table */
|
||||
SELECT @msg
|
||||
= @msg + 'Duplicate index (on expected index name) found in indexes definition. Table ['
|
||||
+ [itm].[IA_schema_name] + '].[' + [itm].[IA_table_name] + '], expected index name ['
|
||||
+ [itm].[IA_expected_index_name] + ']' + CHAR(13) + CHAR(10)
|
||||
+ [itm].[IA_schemaName] + '].[' + [itm].[IA_tableName] + '], expected index name ['
|
||||
+ [itm].[IA_expectedIndexName] + ']' + CHAR(13) + CHAR(10)
|
||||
FROM [upd].[IA_IndexesAlignementActions] [itm]
|
||||
JOIN
|
||||
(
|
||||
SELECT [ii].[IA_expected_index_name] AS [key]
|
||||
SELECT [ii].[IA_expectedIndexName] AS [key]
|
||||
FROM [upd].[IA_IndexesAlignementActions] [ii]
|
||||
GROUP BY [ii].[IA_expected_index_name]
|
||||
GROUP BY [ii].[IA_expectedIndexName]
|
||||
HAVING COUNT(1) > 1
|
||||
) d
|
||||
ON d.[key] = [itm].[IA_expected_index_name]
|
||||
GROUP BY [itm].[IA_schema_name],
|
||||
[itm].[IA_table_name],
|
||||
[itm].[IA_expected_index_name];
|
||||
ON d.[key] = [itm].[IA_expectedIndexName]
|
||||
GROUP BY [itm].[IA_schemaName],
|
||||
[itm].[IA_tableName],
|
||||
[itm].[IA_expectedIndexName];
|
||||
|
||||
--duplicate index key
|
||||
SELECT @msg
|
||||
= @msg + 'Duplicate index (on column definition) found in indexes definition. Table [' + [itm].[IA_schema_name]
|
||||
+ '].[' + [itm].[IA_table_name] + '], columns in index: ' + [itm].[IA_columns_name]
|
||||
= @msg + 'Duplicate index (on column definition) found in indexes definition. Table [' + [itm].[IA_schemaName]
|
||||
+ '].[' + [itm].[IA_tableName] + '], columns in index: ' + [itm].[IA_columnsName]
|
||||
+ CASE
|
||||
WHEN [itm].[IA_included_columns] IS NOT NULL THEN
|
||||
', included colum(s): ' + [itm].[IA_included_columns]
|
||||
WHEN [itm].[IA_includedColumns] IS NOT NULL THEN
|
||||
', included colum(s): ' + [itm].[IA_includedColumns]
|
||||
ELSE
|
||||
''
|
||||
END + CHAR(13) + CHAR(10)
|
||||
FROM [upd].[IA_IndexesAlignementActions] [itm]
|
||||
JOIN
|
||||
(
|
||||
SELECT [ii].[IA_columns_name] + '_' + ISNULL([ii].[IA_included_columns], '') AS [key]
|
||||
SELECT [ii].[IA_columnsName] + '_' + ISNULL([ii].[IA_includedColumns], '') AS [key]
|
||||
FROM [upd].[IA_IndexesAlignementActions] [ii]
|
||||
GROUP BY [ii].[IA_columns_name] + '_' + ISNULL([ii].[IA_included_columns], '')
|
||||
GROUP BY [ii].[IA_columnsName] + '_' + ISNULL([ii].[IA_includedColumns], '')
|
||||
HAVING COUNT(1) > 1
|
||||
) d
|
||||
ON d.[key] = [itm].[IA_columns_name] + '_' + ISNULL([itm].[IA_included_columns], '')
|
||||
GROUP BY [itm].[IA_schema_name],
|
||||
[itm].[IA_table_name],
|
||||
[itm].[IA_columns_name],
|
||||
[itm].[IA_included_columns];
|
||||
ON d.[key] = [itm].[IA_columnsName] + '_' + ISNULL([itm].[IA_includedColumns], '')
|
||||
GROUP BY [itm].[IA_schemaName],
|
||||
[itm].[IA_tableName],
|
||||
[itm].[IA_columnsName],
|
||||
[itm].[IA_includedColumns];
|
||||
|
||||
IF @msg <> ''
|
||||
BEGIN
|
||||
@@ -182,11 +180,15 @@ BEGIN
|
||||
|
||||
|
||||
/* fetch the indexes name and non listed indexes on the current instance */
|
||||
DECLARE curIdxName CURSOR FAST_FORWARD READ_ONLY FOR
|
||||
IF CURSOR_STATUS('global','curIdxName')>=-1
|
||||
BEGIN
|
||||
DEALLOCATE curIdxName
|
||||
END
|
||||
DECLARE curIdxName CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
|
||||
SELECT DISTINCT
|
||||
[itm].[IA_schema_name],
|
||||
[itm].[IA_table_name],
|
||||
itm.[IA_indexesAlignementActions_id]
|
||||
[itm].[IA_schemaName],
|
||||
[itm].[IA_tableName],
|
||||
itm.[IA_indexesAlignementActionsID]
|
||||
FROM [upd].[IA_IndexesAlignementActions] [itm];
|
||||
|
||||
OPEN curIdxName;
|
||||
@@ -577,13 +579,6 @@ BEGIN
|
||||
FROM @indexesToMaintains [itm]
|
||||
WHERE [isToBeRecreated_inp] = 1;
|
||||
|
||||
--/* to be re-created because wrong index type*/
|
||||
--UPDATE [itm]
|
||||
--SET [itm].[isToBeRecreated_comp] = 1
|
||||
--FROM @indexesToMaintains [itm]
|
||||
--WHERE [itm].isClustered_inp = 1
|
||||
--AND itm.;
|
||||
|
||||
/* to be deleted, because specified in table */
|
||||
UPDATE itm
|
||||
SET [itm].[isToBeDeleted_comp] = CASE
|
||||
@@ -609,8 +604,8 @@ BEGIN
|
||||
AND [exp].[isToBeRecreated_inp] = 0
|
||||
AND
|
||||
(
|
||||
[exp].[columnsName_inp] != [act].[actualColumns_internal]
|
||||
OR [exp].[includedColumns_inp] != [act].[actualIncludedColumns_internal]
|
||||
[exp].[columnsName_inp] <> [act].[actualColumns_internal]
|
||||
OR [exp].[includedColumns_inp] <> [act].[actualIncludedColumns_internal]
|
||||
);
|
||||
|
||||
UPDATE [act]
|
||||
@@ -619,8 +614,8 @@ BEGIN
|
||||
JOIN @indexesToMaintains [act] --actual
|
||||
ON [act].[indexName] = [exp].[expectedIndexName_inp]
|
||||
WHERE (
|
||||
[exp].[columnsName_inp] != [act].[actualColumns_internal]
|
||||
OR [exp].[includedColumns_inp] != [act].[actualIncludedColumns_internal]
|
||||
[exp].[columnsName_inp] <> [act].[actualColumns_internal]
|
||||
OR [exp].[includedColumns_inp] <> [act].[actualIncludedColumns_internal]
|
||||
);
|
||||
|
||||
/* to be deleted, because different to specs. link on index columns */
|
||||
@@ -630,8 +625,8 @@ BEGIN
|
||||
JOIN @indexesToMaintains [act] --actual
|
||||
ON [act].[actualColumns_internal] = [exp].[columnsName_inp]
|
||||
WHERE (
|
||||
[exp].[columnsName_inp] != [act].[actualColumns_internal]
|
||||
OR [exp].[includedColumns_inp] != [act].[actualIncludedColumns_internal]
|
||||
[exp].[columnsName_inp] <> [act].[actualColumns_internal]
|
||||
OR [exp].[includedColumns_inp] <> [act].[actualIncludedColumns_internal]
|
||||
);
|
||||
|
||||
UPDATE [act]
|
||||
@@ -640,8 +635,8 @@ BEGIN
|
||||
JOIN @indexesToMaintains [act] --actual
|
||||
ON [act].[actualColumns_internal] = [exp].[columnsName_inp]
|
||||
WHERE (
|
||||
[exp].[columnsName_inp] != [act].[actualColumns_internal]
|
||||
OR [exp].[includedColumns_inp] != [act].[actualIncludedColumns_internal]
|
||||
[exp].[columnsName_inp] <> [act].[actualColumns_internal]
|
||||
OR [exp].[includedColumns_inp] <> [act].[actualIncludedColumns_internal]
|
||||
);
|
||||
|
||||
/* if we have both "rename" and "delete" actions, remove the "rename" action */
|
||||
@@ -794,7 +789,7 @@ BEGIN
|
||||
ia.[IA_statusMsg] = ii.statusMsg_internal
|
||||
FROM [upd].[IA_IndexesAlignementActions] ia
|
||||
JOIN @indexesToMaintains ii
|
||||
ON ii.[IA_indexesAlignementActions] = ia.[IA_indexesAlignementActions_id]
|
||||
ON ii.[IA_indexesAlignementActions] = ia.[IA_indexesAlignementActionsID]
|
||||
WHERE ia.[IA_executionDate] IS NULL;
|
||||
END;
|
||||
GO
|
||||
|
||||
Reference in New Issue
Block a user