This commit is contained in:
2023-02-22 09:06:46 +01:00
parent 911e4e6720
commit 5874709908
22 changed files with 1081 additions and 314 deletions

View File

@@ -15,6 +15,18 @@
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
</ConnectionNode>
<ConnectionNode Name="swdba01.centralinfra.net\PGALDBA:CENTRALINFRA\ua208700">
<Created>2023-02-14T14:36:15.2187218+01:00</Created>
<Type>SQL</Type>
<Server>swdba01.centralinfra.net\PGALDBA</Server>
<UserName />
<Authentication>Windows Authentication</Authentication>
<InitialDB />
<LoginTimeout>30</LoginTimeout>
<ExecutionTimeout>0</ExecutionTimeout>
<ConnectionProtocol>NotSpecified</ConnectionProtocol>
<ApplicationName>Microsoft SQL Server Management Studio - Query</ApplicationName>
</ConnectionNode>
</Items>
</LogicalFolder>
<LogicalFolder Name="Queries" Type="0" Sorted="true">

View File

@@ -1,14 +1,14 @@
USE Arizona;
GO
DELETE FROM upd.IA_IndexesAlignementActions
DELETE FROM upd.IA_IndexAlignementActions
WHERE IA_schema_name = 'dbo.Criteria';
BEGIN TRANSACTION;
WITH ColInfo
AS (SELECT TblName = o.name,
SchemaTbl = s.name + '.' + o.name,
IndexName = i.name,
AS (SELECT TblName = [o].[name],
SchemaTbl = s.name + '.' + [o].[name],
IndexName = [i].[name],
s.name AS schemaName,
IsPrimaryKey = i.is_primary_key,
IsUniqueConstraint = i.is_unique_constraint,
@@ -21,29 +21,29 @@ AS (SELECT TblName = o.name,
END AS is_clustered,
ColName = c.name,
IsComputedCol = c.is_computed,
IsIncludedCol = ic.is_included_column,
ic.key_ordinal,
IsIncludedCol = [ic].is_included_column,
[ic].key_ordinal,
FilterDefinition = i.filter_definition,
i.object_id,
i.ignore_dup_key,
i.fill_factor,
i.is_padded
FROM sys.objects o
JOIN sys.schemas s
FROM [sys].objects o
JOIN [sys].schemas s
ON o.schema_id = s.schema_id
JOIN sys.columns c
JOIN [sys].columns c
ON o.object_id = c.object_id
JOIN sys.indexes i
JOIN [sys].indexes i
ON c.object_id = i.object_id
JOIN sys.index_columns ic
ON i.index_id = ic.index_id
AND o.object_id = ic.object_id
AND c.column_id = ic.column_id
WHERE o.name = 'criteria'
JOIN [sys].index_columns ic
ON i.index_id = [ic].index_id
AND o.object_id = [ic].object_id
AND c.column_id = [ic].column_id
WHERE [o].[name] = 'criteria'
AND s.name = 'dbo')
SELECT DISTINCT
'
INSERT INTO arizona.upd.IA_IndexesAlignementActions
INSERT INTO arizona.upd.IA_IndexAlignementActions
(
IA_executionDate,
IA_schema_name,
@@ -65,16 +65,17 @@ INSERT INTO arizona.upd.IA_IndexesAlignementActions
IA_actualColumns,
IA_actualIncludedColumns,
IA_statusMsg,
IA_index_options
IA_index_options,
IA_reference
)
SELECT
NULL as IA_executionDate,
''' + x.schemaName + ''' as IA_schema_name,
''' + x.TblName + ''' as IA_table_name,
''' + [x].schemaName + ''' as IA_schema_name,
''' + [x].TblName + ''' as IA_table_name,
''' + c.IndexColumns + ''' as IA_columns_name,
0 as IA_is_clustered,
' + CASE
WHEN x.IsUnique = 0 THEN
WHEN [x].IsUnique = 0 THEN
'0'
ELSE
'1'
@@ -87,7 +88,7 @@ SELECT
END + ' as IA_included_columns,
0 as IA_is_to_be_deleted,
0 as IA_is_to_be_recreated,
''' + x.IndexName
''' + [x].IndexName
+ ''' as IA_expected_index_name,
NULL as IA_foundIndexName,
0 as IA_wasIndexFound,
@@ -100,28 +101,29 @@ SELECT
NULL as IA_actualIncludedColumns,
NULL as IA_statusMsg,
''' + CASE
WHEN INDEXPROPERTY(x.object_id, x.IndexName, 'IsStatistics') = 1 THEN
WHEN INDEXPROPERTY([x].object_id, [x].IndexName, 'IsStatistics') = 1 THEN
'STATISTICS_NORECOMPUTE = ON, '
ELSE
'STATISTICS_NORECOMPUTE = OFF, '
END + CASE
WHEN x.ignore_dup_key = 1 THEN
WHEN [x].ignore_dup_key = 1 THEN
'IGNORE_DUP_KEY = ON, '
ELSE
'IGNORE_DUP_KEY = OFF, '
END + CASE
WHEN x.is_padded = 1 THEN
WHEN [x].is_padded = 1 THEN
'PAD_INDEX = ON,'
ELSE
''
END + 'SORT_IN_TEMPDB = OFF, FILLFACTOR =' + CAST(x.fill_factor AS VARCHAR(3))
+ ''' as IA_index_options
END + 'SORT_IN_TEMPDB = OFF, FILLFACTOR =' + CAST([x].fill_factor AS VARCHAR(3))
+ ''' as IA_index_options,
NULL as IA_reference
WHERE NOT EXISTS(
SELECT 1
FROM arizona.upd.IA_IndexesAlignementActions s
FROM arizona.upd.IA_IndexAlignementActions s
WHERE s.IA_executionDate IS NULL
AND s.IA_schema_name = ''' + x.schemaName + '''
AND s.IA_table_name = ''' + x.TblName + '''
AND s.IA_schema_name = ''' + [x].schemaName + '''
AND s.IA_table_name = ''' + [x].TblName + '''
AND s.IA_columns_name = ''' + c.IndexColumns + '''
' + CASE
WHEN ci.IncludedColumns IS NOT NULL THEN
@@ -131,37 +133,37 @@ WHERE NOT EXISTS(
END + '
)
' AS cmd,
x.IndexName,
[x].IndexName,
c.IndexColumns,
ci.IncludedColumns
FROM ColInfo x
CROSS APPLY
(
SELECT IndexColumns = STUFF(sq.strXML, 1, 2, '')
SELECT IndexColumns = STUFF([sq].strXML, 1, 2, '')
FROM
(
SELECT ', ' + ISNULL(x2.ColName, '')
SELECT ', ' + ISNULL([x2].ColName, '')
FROM ColInfo x2
WHERE x.TblName = x2.TblName
AND x.schemaName = x2.schemaName
AND x.IndexName = x2.IndexName
AND x2.IsIncludedCol = 0
ORDER BY x2.key_ordinal
WHERE [x].TblName = [x2].TblName
AND [x].schemaName = [x2].schemaName
AND [x].IndexName = [x2].IndexName
AND [x2].IsIncludedCol = 0
ORDER BY [x2].key_ordinal
FOR XML PATH('')
) sq(strXML)
) c
OUTER APPLY
(
SELECT IncludedColumns = STUFF(sq.strXML, 1, 2, '')
SELECT IncludedColumns = STUFF([sq].strXML, 1, 2, '')
FROM
(
SELECT ', ' + ISNULL(x2.ColName, '')
SELECT ', ' + ISNULL([x2].ColName, '')
FROM ColInfo x2
WHERE x.TblName = x2.TblName
AND x.schemaName = x2.schemaName
AND x.IndexName = x2.IndexName
AND x2.IsIncludedCol = 1
ORDER BY x2.key_ordinal
WHERE [x].TblName = [x2].TblName
AND [x].schemaName = [x2].schemaName
AND [x].IndexName = [x2].IndexName
AND [x2].IsIncludedCol = 1
ORDER BY [x2].key_ordinal
FOR XML PATH('')
) sq(strXML)
) ci;

View File

@@ -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

View File

@@ -1,82 +1,223 @@
USE Arizona
IF OBJECT_ID('upd.IA_IndexesAlignementActions') IS NOT NULL
DROP TABLE upd.IA_IndexesAlignementActions;
IF OBJECT_ID('upd.IA_IndexAlignementActions') IS NOT NULL
DROP TABLE upd.IA_IndexAlignementActions;
GO
CREATE TABLE upd.IA_IndexesAlignementActions(
IA_indexesAlignementActions_id INT NOT NULL IDENTITY(1,1)
,IA_executionDate DATETIME2(0) NULL /* The date and time the action was executed, null if the action is to be executed */
,IA_schema_name VARCHAR(100) NOT NULL /* the schema the table is part of */
,IA_table_name VARCHAR(100) NOT NULL /* On which table is the index, whithout the schema */
,IA_columns_name VARCHAR(MAX) NOT NULL /* list all the columns of the index, in the correct orders */
,IA_is_clustered BIT NOT NULL /* Is the indexe a clustered index ? */
,IA_is_unique BIT NOT NULL /* Is the index unique ? */
,IA_included_columns VARCHAR(MAX)NULL /* list all the included columns the index should have. */
,IA_index_options VARCHAR(MAX) NULL /* Give here the index options if any. they will be applied on index creation but they are not part of the delta checks */
,IA_is_to_be_deleted BIT NOT NULL /* must the script drop this index ? */
,IA_is_to_be_recreated BIT NOT NULL /* must the script recreate this index ?
If an existing index differs from the definition, it will be dropped and the correct one created.
Use this only to force the re-creation even if the structure is identical */
,IA_expected_index_name VARCHAR(500) NOT NULL /* the name the index should have, used to determine if a rename of the existing index should be done */
/* everything below is a log of past changes */
,[IA_foundIndexName] VARCHAR(MAX) NULL /* computed by the script. The index name on this system, as indexes name can be different accross systems*/
,[IA_wasIndexFound] BIT NOT NULL /* computed by the script. set to 1 if the index is present on the server */
CONSTRAINT DF_IA_wasIndexFound DEFAULT 0
,[IA_wasCreated] BIT NULL /* computed by the script. does this index was created on the server ? */
CONSTRAINT DF_IA_wasCreated DEFAULT 0
,[IA_wasRenamed] BIT NULL /* computed by the script. does this index was renamed ? */
CONSTRAINT DF_IA_wasRenamed DEFAULT 0
,[IA_wasDeleted] BIT NULL /* computed by the script. does this index will be deleted ? */
CONSTRAINT DF_IA_wasDeleted DEFAULT 0
,[IA_wasRecreated] BIT NULL /* computed by the script. does this index will be re-created ? */
CONSTRAINT DF_IA_wasRecreated DEFAULT 0
,[IA_includedColumnsMatch] BIT NULL /* computed by the script. Are the expected and actual included columns identical ? */
,[IA_actualColumns] VARCHAR(MAX) NULL /* the actual columns in the existing index */
,[IA_actualIncludedColumns] VARCHAR(MAX) NULL /* the actual columns in the INCLUDE part of the index */
,[IA_statusMsg] VARCHAR(MAX) NULL /* Message, used for debug */
,CONSTRAINT PK_IA_IndexesAlignementActions PRIMARY KEY(IA_indexesAlignementActions_id)
);
ALTER TABLE upd.IA_IndexesAlignementActions ADD CONSTRAINT DF_IA_is_clustered DEFAULT 0 FOR IA_is_clustered;
ALTER TABLE upd.IA_IndexesAlignementActions ADD CONSTRAINT DF_IA_is_unique DEFAULT 0 FOR IA_is_unique;
ALTER TABLE upd.IA_IndexesAlignementActions ADD CONSTRAINT DF_IA_is_to_be_deleted DEFAULT 0 FOR IA_is_to_be_deleted;
ALTER TABLE upd.IA_IndexesAlignementActions ADD CONSTRAINT DF_IA_is_to_be_recreated DEFAULT 0 FOR IA_is_to_be_recreated;
CREATE UNIQUE NONCLUSTERED INDEX NCUIX_IA_IndexesAlignementActions_index_name ON upd.IA_IndexesAlignementActions(IA_expected_index_name);
IF OBJECT_ID('upd.IA_IndexAlignementActions') IS NOT NULL
DROP TABLE upd.IA_IndexAlignementActions;
GO
INSERT INTO upd.IA_IndexesAlignementActions (
IA_columns_name,
IA_is_to_be_deleted,
IA_is_to_be_recreated,
IA_expected_index_name,
IA_included_columns,
[IA_schema_name],
[IA_table_name],
[IA_is_clustered],
[IA_is_unique]
/* OCTPDBA-414 */
IF OBJECT_ID('upd.IA_IndexAlignementActions') IS NULL
BEGIN
CREATE TABLE upd.IA_IndexAlignementActions(
IA_indexAlignementActionsID INT NOT NULL IDENTITY(1,1)
,IA_executionDate DATETIME2(0) NULL /* The date and time the action was executed, null if the action is to be executed */
,IA_schemaName VARCHAR(100) NOT NULL /* the schema the table is part of */
,IA_tableName VARCHAR(100) NOT NULL /* On which table is the index, whithout the schema */
,IA_columnsName VARCHAR(MAX) NOT NULL /* list all the columns of the index, in the correct orders */
,IA_isClustered BIT NOT NULL /* Is the indexe a clustered index ? */
CONSTRAINT DF_IA_isClustered DEFAULT 0
,IA_isUnique BIT NOT NULL /* Is the index unique ? */
CONSTRAINT DF_IA_isUnique DEFAULT 0
,IA_includedColumns VARCHAR(MAX)NULL /* list all the included columns the index should have. */
,IA_indexOptions VARCHAR(MAX) NULL /* Give here the index options if any. they will be applied on index creation but they are not part of the delta checks */
,IA_isToBeDeleted BIT NOT NULL /* must the script drop this index ? */
CONSTRAINT DF_IA_isToBeDeleted DEFAULT 0
,IA_isToBeRecreated BIT NOT NULL /* must the script recreate this index ? */
CONSTRAINT DF_IA_isToBeRecreated DEFAULT 0 /* If an existing index differs from the definition, it will be dropped and the correct one created. */
/* Use this only to force the re-creation even if the structure is identical */
,IA_expectedIndexName VARCHAR(500) NOT NULL /* the name the index should have, used to determine if a rename of the existing index should be done */
,IA_reference VARCHAR(500) NULL /* A free text that can contain a reference. Seen to simplify update scripts */
/* everything below is a log of past changes */
,[IA_foundIndexName] VARCHAR(MAX) NULL /* computed by the script. The index name on this system, as indexes name can be different accross systems*/
,[IA_wasIndexFound] BIT NOT NULL /* computed by the script. set to 1 if the index is present on the server */
CONSTRAINT DF_IA_wasIndexFound DEFAULT 0
,[IA_wasCreated] BIT NOT NULL /* computed by the script. does this index was created on the server ? */
CONSTRAINT DF_IA_wasCreated DEFAULT 0
,[IA_wasRenamed] BIT NOT NULL /* computed by the script. does this index was renamed ? */
CONSTRAINT DF_IA_wasRenamed DEFAULT 0
,[IA_wasDeleted] BIT NOT NULL /* computed by the script. does this index will be deleted ? */
CONSTRAINT DF_IA_wasDeleted DEFAULT 0
,[IA_wasRecreated] BIT NOT NULL /* computed by the script. does this index will be re-created ? */
CONSTRAINT DF_IA_wasRecreated DEFAULT 0
,[IA_includedColumnsMatch] BIT NULL /* computed by the script. Are the expected and actual included columns identical ? */
,[IA_actualColumns] VARCHAR(MAX) NULL /* the actual columns in the existing index */
,[IA_actualIncludedColumns] VARCHAR(MAX) NULL /* the actual columns in the INCLUDE part of the index */
,[IA_statusMsg] VARCHAR(MAX) NULL /* Message, used for debug */
,CONSTRAINT PK_IA_IndexAlignementActions PRIMARY KEY CLUSTERED(IA_indexAlignementActionsID)
);
END
IF NOT EXISTS (
SELECT 1
FROM [sys].all_columns c
JOIN [sys].tables t ON c.object_id = t.object_id
JOIN [sys].schemas s ON t.schema_id = s.schema_id
JOIN [sys].default_constraints dc ON c.default_object_id = dc.object_id
WHERE s.name = 'upd'
AND t.name = 'IA_IndexAlignementActions'
AND c.name = 'IA_isClustered'
)
BEGIN
ALTER TABLE upd.IA_IndexAlignementActions ADD CONSTRAINT DF_IA_isClustered DEFAULT 0 FOR [IA_isClustered];
END;
IF NOT EXISTS (
SELECT 1
FROM [sys].all_columns c
JOIN [sys].tables t ON c.object_id = t.object_id
JOIN [sys].schemas s ON t.schema_id = s.schema_id
JOIN [sys].default_constraints dc ON c.default_object_id = dc.object_id
WHERE s.name = 'upd'
AND t.name = 'IA_IndexAlignementActions'
AND c.name = 'IA_isUnique'
)
BEGIN
ALTER TABLE [upd].[IA_IndexAlignementActions] ADD CONSTRAINT DF_IA_isUnique DEFAULT 0 FOR [IA_isUnique];
END
IF NOT EXISTS (
SELECT 1
FROM [sys].all_columns c
JOIN [sys].tables t ON c.object_id = t.object_id
JOIN [sys].schemas s ON t.schema_id = s.schema_id
JOIN [sys].default_constraints dc ON c.default_object_id = dc.object_id
WHERE s.name = 'upd'
AND t.name = 'IA_IndexAlignementActions'
AND c.name = 'IA_isToBeDeleted'
)
BEGIN
ALTER TABLE [upd].[IA_IndexAlignementActions] ADD CONSTRAINT DF_IA_isToBeDeleted DEFAULT 0 FOR [IA_isToBeDeleted];
END
IF NOT EXISTS (
SELECT 1
FROM [sys].all_columns c
JOIN [sys].tables t ON c.object_id = t.object_id
JOIN [sys].schemas s ON t.schema_id = s.schema_id
JOIN [sys].default_constraints dc ON c.default_object_id = dc.object_id
WHERE s.name = 'upd'
AND t.name = 'IA_IndexAlignementActions'
AND c.name = 'IA_isToBeRecreated'
)
BEGIN
ALTER TABLE [upd].[IA_IndexAlignementActions] ADD CONSTRAINT DF_IA_isToBeRecreated DEFAULT 0 FOR [IA_isToBeRecreated];
END
IF NOT EXISTS ( SELECT 1
FROM [sys].indexes i
WHERE i.object_id = OBJECT_ID(N'upd.IA_IndexAlignementActions')
AND [i].[name] = N'NCUIX_IA_IndexAlignementActions_COL_IA_expectedIndexName')
BEGIN
CREATE UNIQUE NONCLUSTERED INDEX NCUIX_IA_IndexAlignementActions_COL_IA_expectedIndexName ON [upd].[IA_IndexAlignementActions](IA_expectedIndexName);
END
GO
/* Create Table Comments */
EXEC sp_addextendedproperty 'MS_Description', 'The date and time the action was executed, null if the action is to be executed', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_executionDate]
GO
EXEC sp_addextendedproperty 'MS_Description', 'the schema the table is part of', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_schemaName]
GO
EXEC sp_addextendedproperty 'MS_Description', 'On which table is the index, whithout the schema', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_tableName]
GO
EXEC sp_addextendedproperty 'MS_Description', 'list all the columns of the index, in the correct orders', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_columnsName]
GO
EXEC sp_addextendedproperty 'MS_Description', 'Is the index a clustered index ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_isClustered]
GO
EXEC sp_addextendedproperty 'MS_Description', 'Is the index unique ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_isUnique]
GO
EXEC sp_addextendedproperty 'MS_Description', 'list all the included columns the index should have.', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_includedColumns]
GO
EXEC sp_addextendedproperty 'MS_Description', 'Give here the index options if any. they will be applied on index creation but they are not part of the delta checks', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_indexOptions]
GO
EXEC sp_addextendedproperty 'MS_Description', 'must the script drop this index ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_isToBeDeleted]
GO
EXEC sp_addextendedproperty 'MS_Description', 'must the script recreate this index ?
If an existing index differs from the definition, it will be dropped and the correct one created.
Use this only to force the re-creation even if the structure is identical', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_isToBeRecreated]
GO
EXEC sp_addextendedproperty 'MS_Description', 'the name the index should have, used to determine if a rename of the existing index should be done', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_expectedIndexName]
GO
EXEC sp_addextendedproperty 'MS_Description', 'A free text that can contain a reference. Seen to simplify update scripts', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_reference]
GO
EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. The index name on this system, as indexes name can be different accross systems', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_foundIndexName]
GO
EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. set to 1 if the index is present on the server ', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_wasIndexFound]
GO
EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. does this index was created on the server ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_wasCreated]
GO
EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. does this index was renamed ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_wasRenamed]
GO
EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. does this index was deleted ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_wasDeleted]
GO
EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. does this index was re-created ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_wasRecreated]
GO
EXEC sp_addextendedproperty 'MS_Description', 'computed by the proc. Are the expected and actual included columns identical ?', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_includedColumnsMatch]
GO
EXEC sp_addextendedproperty 'MS_Description', 'the actual columns in the existing index', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_actualColumns]
GO
EXEC sp_addextendedproperty 'MS_Description', 'the actual columns in the INCLUDE part of the index', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_actualIncludedColumns]
GO
EXEC sp_addextendedproperty 'MS_Description', 'Message, used for debug', 'Schema', [upd], 'table', [IA_IndexAlignementActions], 'column', [IA_statusMsg]
GO
INSERT INTO upd.IA_IndexAlignementActions (
IA_columnsName,
IA_isToBeDeleted,
IA_isToBeRecreated,
IA_expectedIndexName,
IA_includedColumns,
IA_schemaName,
IA_tableName,
IA_isClustered,
IA_isUnique
,IA_reference
)
/* indexe(s) we do want */
VALUES('Entry_id',0,0,'PK_Entry_id', NULL,'dbo','entry',0,0)
,('ET_batch_run',0,0,'NCIX_Entry_COL_ET_batch_run',NULL,'dbo','entry',0,0)
,('ET_accounting_period',0,0,'NCIX_Entry_COL_ET_accounting_period','Entry_ID','dbo','entry',0,0)
,('ET_document_header',0,0,'NCIX_Entry_COL_ET_document_header','ET_debit_currency_amount, ET_credit_currency_amount','dbo','entry',0,0)
,('ET_account, ET_document_header',0,0,'NCIX_Entry_COL_ET_account','Entry_ID','dbo','entry',0,0)
,('ET_reconciliation_status, ET_account, ET_document_header',0,0,'NCIX_Entry_COL_ET_reconciliation_status','ET_debit_base_amount, ET_credit_base_amount, ET_reconciliation_base_amount','dbo','entry',0,0)
,('ET_predefined_entry',0,0,'NCIX_Entry_COL_ET_predefined_entry',NULL,'dbo','entry',0,0)
,('ET_entry_address',0,0,'NCIX_Entry_COL_ET_entry_address',NULL,'dbo','entry',0,0)
,('ET_currency',0,0,'NCIX_Entry_COL_ET_currency',NULL,'dbo','entry',0,0)
VALUES('Entry_id',0,0,'PK_Entry_id', NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_batch_run',0,0,'NCIX_Entry_COL_ET_batch_run',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_accounting_period',0,0,'NCIX_Entry_COL_ET_accounting_period','Entry_ID','dbo','entry',0,0,'OCTPDBA-365')
,('ET_document_header',0,0,'NCIX_Entry_COL_ET_document_header','ET_debit_currency_amount, ET_credit_currency_amount','dbo','entry',0,0,'OCTPDBA-365')
,('ET_account, ET_document_header',0,0,'NCIX_Entry_COL_ET_account','Entry_ID','dbo','entry',0,0,'OCTPDBA-365')
,('ET_reconciliation_status, ET_account, ET_document_header',0,0,'NCIX_Entry_COL_ET_reconciliation_status','ET_debit_base_amount, ET_credit_base_amount, ET_reconciliation_base_amount','dbo','entry',0,0,'OCTPDBA-365')
,('ET_predefined_entry',0,0,'NCIX_Entry_COL_ET_predefined_entry',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_entry_address',0,0,'NCIX_Entry_COL_ET_entry_address',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_currency',0,0,'NCIX_Entry_COL_ET_currency',NULL,'dbo','entry',0,0,'OCTPDBA-365')
/* indexe(s) used in central, even if not in pharmacies*/
,('ET_entry_type',0,0,'NCIX_Entry_COL_ET_entry_type',NULL,'dbo','entry',0,0)
,('ET_entry_type',0,0,'NCIX_Entry_COL_ET_entry_type',NULL,'dbo','entry',0,0,'OCTPDBA-365')
/* indexe(s) we really want to drop*/
,('ET_master_ID',1,0,'NCIX_Entry_COL_ET_master_ID',NULL,'dbo','entry',0,0)
,('ET_bmc_user_profile',1,0,'NCIX_Entry_COL_ET_bmc_user_profile',NULL,'dbo','entry',0,0)
,('ET_sales_tax_code',1,0,'NCIX_Entry_COL_ET_sales_tax_code',NULL,'dbo','entry',0,0)
,('ET_APS_TS', 1, 0, 'NCIX_Entry_COL_ET_APS_TS',NULL,'dbo','entry',0,0)
,('ET_master_ID',1,0,'NCIX_Entry_COL_ET_master_ID',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_bmc_user_profile',1,0,'NCIX_Entry_COL_ET_bmc_user_profile',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_sales_tax_code',1,0,'NCIX_Entry_COL_ET_sales_tax_code',NULL,'dbo','entry',0,0,'OCTPDBA-365')
,('ET_APS_TS', 1, 0, 'NCIX_Entry_COL_ET_APS_TS',NULL,'dbo','entry',0,0,'OCTPDBA-365')
;

View File

@@ -4,144 +4,298 @@ BEGIN TRANSACTION;
SET XACT_ABORT ON;
INSERT INTO upd.IA_IndexesAlignementActions
(
IA_executionDate,
IA_schema_name,
IA_table_name,
IA_columns_name,
IA_is_clustered,
IA_is_unique,
IA_included_columns,
IA_is_to_be_deleted,
IA_is_to_be_recreated,
IA_expected_index_name,
IA_foundIndexName,
IA_wasIndexFound,
IA_wasCreated,
IA_wasRenamed,
IA_wasDeleted,
IA_wasRecreated,
IA_includedColumnsMatch,
IA_actualColumns,
IA_actualIncludedColumns,
IA_statusMsg,
IA_index_options
)
SELECT
NULL AS IA_executionDate,
'dbo' AS IA_schema_name,
'Criteria' AS IA_table_name,
'CR_code, CR_criteria_type' AS IA_columns_name,
0 AS IA_is_clustered,
1 AS IA_is_unique,
NULL AS IA_included_columns,
0 AS IA_is_to_be_deleted,
0 AS IA_is_to_be_recreated,
'NCIX_Criteria_COL_CR_code' AS IA_expected_index_name,
NULL AS IA_foundIndexName,
0 AS IA_wasIndexFound,
0 AS IA_wasCreated,
0 AS IA_wasRenamed,
0 AS IA_wasDeleted,
0 AS IA_wasRecreated,
NULL AS IA_includedColumnsMatch,
NULL AS IA_actualColumns,
NULL AS IA_actualIncludedColumns,
NULL AS IA_statusMsg ,
NULL AS IA_index_options
UNION
SELECT
NULL AS IA_executionDate,
'dbo' AS IA_schema_name,
'Criteria' AS IA_table_name,
'CR_criteria_type' AS IA_columns_name,
0 AS IA_is_clustered,
0 AS IA_is_unique,
NULL AS IA_included_columns,
0 AS IA_is_to_be_deleted,
0 AS IA_is_to_be_recreated,
'NCIX_Criteria_COL_CR_criteria_type' AS IA_expected_index_name,
NULL AS IA_foundIndexName,
0 AS IA_wasIndexFound,
0 AS IA_wasCreated,
0 AS IA_wasRenamed,
0 AS IA_wasDeleted,
0 AS IA_wasRecreated,
NULL AS IA_includedColumnsMatch,
NULL AS IA_actualColumns,
NULL AS IA_actualIncludedColumns,
NULL AS IA_statusMsg ,
NULL AS IA_index_options
UNION
SELECT
NULL AS IA_executionDate,
'dbo' AS IA_schema_name,
'Criteria' AS IA_table_name,
'CR_master_ID' AS IA_columns_name,
0 AS IA_is_clustered,
0 AS IA_is_unique,
NULL AS IA_included_columns,
0 AS IA_is_to_be_deleted,
01 AS IA_is_to_be_recreated,
'NCIX_Criteria_COL_CR_master_ID' AS IA_expected_index_name,
NULL AS IA_foundIndexName,
0 AS IA_wasIndexFound,
0 AS IA_wasCreated,
0 AS IA_wasRenamed,
0 AS IA_wasDeleted,
0 AS IA_wasRecreated,
NULL AS IA_includedColumnsMatch,
NULL AS IA_actualColumns,
NULL AS IA_actualIncludedColumns,
NULL AS IA_statusMsg,
NULL AS IA_index_options
UNION
SELECT
NULL AS IA_executionDate,
'dbo' AS IA_schema_name,
'Criteria' AS IA_table_name,
'CR_VGUID' AS IA_columns_name,
0 AS IA_is_clustered,
0 AS IA_is_unique,
NULL AS IA_included_columns,
0 AS IA_is_to_be_deleted,
0 AS IA_is_to_be_recreated,
'NCIX_Criteria_COL_CR_VGUID' AS IA_expected_index_name,
NULL AS IA_foundIndexName,
0 AS IA_wasIndexFound,
0 AS IA_wasCreated,
0 AS IA_wasRenamed,
0 AS IA_wasDeleted,
0 AS IA_wasRecreated,
NULL AS IA_includedColumnsMatch,
NULL AS IA_actualColumns,
NULL AS IA_actualIncludedColumns,
NULL AS IA_statusMsg ,
NULL AS IA_index_options
UNION
SELECT
NULL AS IA_executionDate,
'dbo' AS IA_schema_name,
'Criteria' AS IA_table_name,
'Criteria_ID' AS IA_columns_name,
1 AS IA_is_clustered,
1 AS IA_is_unique,
NULL AS IA_included_columns,
0 AS IA_is_to_be_deleted,
0 AS IA_is_to_be_recreated,
'PK_Criteria_ID' AS IA_expected_index_name,
NULL AS IA_foundIndexName,
0 AS IA_wasIndexFound,
0 AS IA_wasCreated,
0 AS IA_wasRenamed,
0 AS IA_wasDeleted,
0 AS IA_wasRecreated,
NULL AS IA_includedColumnsMatch,
NULL AS IA_actualColumns,
NULL AS IA_actualIncludedColumns,
NULL AS IA_statusMsg ,
NULL AS IA_index_options
/* OCTPDBA-414: table dbo.criteria, pharmacy only*/
/* CR_code, CR_criteria_type */
INSERT INTO [Arizona].[upd].[IA_IndexAlignementActions]
(
[IA_executionDate],
[IA_schemaName],
[IA_tableName],
[IA_columnsName],
[IA_isClustered],
[IA_isUnique],
[IA_includedColumns],
[IA_isToBeDeleted],
[IA_isToBeRecreated],
[IA_expectedIndexName],
[IA_foundIndexName],
[IA_wasIndexFound],
[IA_wasCreated],
[IA_wasRenamed],
[IA_wasDeleted],
[IA_wasRecreated],
[IA_includedColumnsMatch],
[IA_actualColumns],
[IA_actualIncludedColumns],
[IA_statusMsg],
[IA_indexOptions],
[IA_reference]
)
SELECT
NULL as [IA_executionDate],
'dbo' as [IA_schemaName],
'Criteria' as [IA_tableName],
'CR_code, CR_criteria_type' as [IA_columnsName],
0 as [IA_isClustered],
1 as [IA_isUnique],
NULL as [IA_includedColumns],
0 as [IA_isToBeRecreated],
0 as [IA_isToBeRecreated],
'NCIX_Criteria_COL_CR_code' as [IA_expectedIndexName],
NULL as [IA_foundIndexName],
0 as [IA_wasIndexFound],
0 as [IA_wasCreated],
0 as [IA_wasRenamed],
0 as [IA_wasDeleted],
0 as [IA_wasRecreated],
NULL as [IA_includedColumnsMatch],
NULL as [IA_actualColumns],
NULL as [IA_actualIncludedColumns],
NULL as [IA_statusMsg],
'STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, SORT_IN_TEMPDB = OFF, FILLFACTOR =90' as IA_index_options,
'OCTPDBA-414' AS [IA_reference]
WHERE NOT EXISTS(
SELECT 1
FROM [Arizona].[upd].[IA_IndexAlignementActions] s
WHERE [s].[IA_schemaName] = 'dbo'
AND [s].[IA_tableName] = 'Criteria'
AND [s].[IA_columnsName] = 'CR_code, CR_criteria_type'
AND [s].[IA_reference] = 'OCTPDBA-414'
)
/* CR_criteria_type */
INSERT INTO [Arizona].[upd].[IA_IndexAlignementActions]
(
[IA_executionDate],
[IA_schemaName],
[IA_tableName],
[IA_columnsName],
[IA_isClustered],
[IA_isUnique],
[IA_includedColumns],
[IA_isToBeDeleted],
[IA_isToBeRecreated],
[IA_expectedIndexName],
[IA_foundIndexName],
[IA_wasIndexFound],
[IA_wasCreated],
[IA_wasRenamed],
[IA_wasDeleted],
[IA_wasRecreated],
[IA_includedColumnsMatch],
[IA_actualColumns],
[IA_actualIncludedColumns],
[IA_statusMsg],
[IA_indexOptions],
[IA_reference]
)
SELECT
NULL as [IA_executionDate],
'dbo' as [IA_schemaName],
'Criteria' as [IA_tableName],
'CR_criteria_type' as [IA_columnsName],
0 as [IA_isClustered],
0 as [IA_isUnique],
NULL as [IA_includedColumns],
0 as [IA_isToBeRecreated],
0 as [IA_isToBeRecreated],
'NCIX_Criteria_COL_CR_criteria_type' as [IA_expectedIndexName],
NULL as [IA_foundIndexName],
0 as [IA_wasIndexFound],
0 as [IA_wasCreated],
0 as [IA_wasRenamed],
0 as [IA_wasDeleted],
0 as [IA_wasRecreated],
NULL as [IA_includedColumnsMatch],
NULL as [IA_actualColumns],
NULL as [IA_actualIncludedColumns],
NULL as [IA_statusMsg],
'STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, SORT_IN_TEMPDB = OFF, FILLFACTOR =90' as IA_index_options,
'OCTPDBA-414' AS [IA_reference]
WHERE NOT EXISTS(
SELECT 1
FROM [Arizona].[upd].[IA_IndexAlignementActions] s
WHERE [s].[IA_schemaName] = 'dbo'
AND [s].[IA_tableName] = 'Criteria'
AND [s].[IA_columnsName] = 'CR_criteria_type'
AND [s].[IA_reference] = 'OCTPDBA-414'
)
/* CR_master_ID */
INSERT INTO [Arizona].[upd].[IA_IndexAlignementActions]
(
[IA_executionDate],
[IA_schemaName],
[IA_tableName],
[IA_columnsName],
[IA_isClustered],
[IA_isUnique],
[IA_includedColumns],
[IA_isToBeDeleted],
[IA_isToBeRecreated],
[IA_expectedIndexName],
[IA_foundIndexName],
[IA_wasIndexFound],
[IA_wasCreated],
[IA_wasRenamed],
[IA_wasDeleted],
[IA_wasRecreated],
[IA_includedColumnsMatch],
[IA_actualColumns],
[IA_actualIncludedColumns],
[IA_statusMsg],
[IA_indexOptions],
[IA_reference]
)
SELECT
NULL as [IA_executionDate],
'dbo' as [IA_schemaName],
'Criteria' as [IA_tableName],
'CR_master_ID' as [IA_columnsName],
0 as [IA_isClustered],
0 as [IA_isUnique],
NULL as [IA_includedColumns],
0 as [IA_isToBeRecreated],
0 as [IA_isToBeRecreated],
'NCIX_Criteria_COL_CR_master_ID' as [IA_expectedIndexName],
NULL as [IA_foundIndexName],
0 as [IA_wasIndexFound],
0 as [IA_wasCreated],
0 as [IA_wasRenamed],
0 as [IA_wasDeleted],
0 as [IA_wasRecreated],
NULL as [IA_includedColumnsMatch],
NULL as [IA_actualColumns],
NULL as [IA_actualIncludedColumns],
NULL as [IA_statusMsg],
'STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, SORT_IN_TEMPDB = OFF, FILLFACTOR =90' as IA_index_options,
'OCTPDBA-414' AS [IA_reference]
WHERE NOT EXISTS(
SELECT 1
FROM [Arizona].[upd].[IA_IndexAlignementActions] s
WHERE [s].[IA_schemaName] = 'dbo'
AND [s].[IA_tableName] = 'Criteria'
AND [s].[IA_columnsName] = 'CR_master_ID'
AND [s].[IA_reference] = 'OCTPDBA-414'
)
/* CR_VGUID */
INSERT INTO [Arizona].[upd].[IA_IndexAlignementActions]
(
[IA_executionDate],
[IA_schemaName],
[IA_tableName],
[IA_columnsName],
[IA_isClustered],
[IA_isUnique],
[IA_includedColumns],
[IA_isToBeDeleted],
[IA_isToBeRecreated],
[IA_expectedIndexName],
[IA_foundIndexName],
[IA_wasIndexFound],
[IA_wasCreated],
[IA_wasRenamed],
[IA_wasDeleted],
[IA_wasRecreated],
[IA_includedColumnsMatch],
[IA_actualColumns],
[IA_actualIncludedColumns],
[IA_statusMsg],
[IA_indexOptions],
[IA_reference]
)
SELECT
NULL as [IA_executionDate],
'dbo' as [IA_schemaName],
'Criteria' as [IA_tableName],
'CR_VGUID' as [IA_columnsName],
0 as [IA_isClustered],
0 as [IA_isUnique],
NULL as [IA_includedColumns],
0 as [IA_isToBeRecreated],
0 as [IA_isToBeRecreated],
'NCIX_Criteria_COL_CR_VGUID' as [IA_expectedIndexName],
NULL as [IA_foundIndexName],
0 as [IA_wasIndexFound],
0 as [IA_wasCreated],
0 as [IA_wasRenamed],
0 as [IA_wasDeleted],
0 as [IA_wasRecreated],
NULL as [IA_includedColumnsMatch],
NULL as [IA_actualColumns],
NULL as [IA_actualIncludedColumns],
NULL as [IA_statusMsg],
'STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, SORT_IN_TEMPDB = OFF, FILLFACTOR =90' as IA_index_options,
'OCTPDBA-414' AS [IA_reference]
WHERE NOT EXISTS(
SELECT 1
FROM [Arizona].[upd].[IA_IndexAlignementActions] s
WHERE [s].[IA_schemaName] = 'dbo'
AND [s].[IA_tableName] = 'Criteria'
AND [s].[IA_columnsName] = 'CR_VGUID'
AND [s].[IA_reference] = 'OCTPDBA-414'
)
/* Criteria_ID */
INSERT INTO [Arizona].[upd].[IA_IndexAlignementActions]
(
[IA_executionDate],
[IA_schemaName],
[IA_tableName],
[IA_columnsName],
[IA_isClustered],
[IA_isUnique],
[IA_includedColumns],
[IA_isToBeDeleted],
[IA_isToBeRecreated],
[IA_expectedIndexName],
[IA_foundIndexName],
[IA_wasIndexFound],
[IA_wasCreated],
[IA_wasRenamed],
[IA_wasDeleted],
[IA_wasRecreated],
[IA_includedColumnsMatch],
[IA_actualColumns],
[IA_actualIncludedColumns],
[IA_statusMsg],
[IA_indexOptions],
[IA_reference]
)
SELECT
NULL as [IA_executionDate],
'dbo' as [IA_schemaName],
'Criteria' as [IA_tableName],
'Criteria_ID' as [IA_columnsName],
0 as [IA_isClustered],
1 as [IA_isUnique],
NULL as [IA_includedColumns],
0 as [IA_isToBeRecreated],
0 as [IA_isToBeRecreated],
'PK_Criteria_ID' as [IA_expectedIndexName],
NULL as [IA_foundIndexName],
0 as [IA_wasIndexFound],
0 as [IA_wasCreated],
0 as [IA_wasRenamed],
0 as [IA_wasDeleted],
0 as [IA_wasRecreated],
NULL as [IA_includedColumnsMatch],
NULL as [IA_actualColumns],
NULL as [IA_actualIncludedColumns],
NULL as [IA_statusMsg],
'STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, SORT_IN_TEMPDB = OFF, FILLFACTOR =90' as IA_index_options,
'OCTPDBA-414' AS [IA_reference]
WHERE NOT EXISTS(
SELECT 1
FROM [Arizona].[upd].[IA_IndexAlignementActions] s
WHERE [s].[IA_schemaName] = 'dbo'
AND [s].[IA_tableName] = 'Criteria'
AND [s].[IA_reference] = 'OCTPDBA-414'
AND [s].[IA_columnsName] = 'Criteria_ID'
)
@@ -151,6 +305,6 @@ EXEC [upd].[alignIndexes] @in_debug = 01, -- int
@in_dropUnreferencedIndexes = NULL; -- bit
SELECT *
FROM upd.IA_IndexesAlignementActions;
FROM upd.IA_IndexAlignementActions;
ROLLBACK TRANSACTION;