Differences
This shows you the differences between two versions of the page.
Last revision Both sides next revision | |||
check_and_repair_primary_keys [2019/01/30 09:22] 127.0.0.1 external edit |
check_and_repair_primary_keys [2019/02/20 11:03] kcifreo |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | + | Explanation of SQL | |
+ | This query will check all the tables in a database to ensure that the primary keys exist and are valid for Control table. | ||
- | + | | |
- | Explanation of SQL | + | |
- | + | ||
- | + | ||
- | + | ||
- | This query will check all the tables in a database to ensure that the primary keys exist and are valid for Control table. | + | |
- | | + | |
* If no valid primary key is found, or an invalid primary is found and deleted, a valid primary key will be created. | * If no valid primary key is found, or an invalid primary is found and deleted, a valid primary key will be created. | ||
* A valid primary key is a clustered index on the ID field (or ID and ClassTypeID is just the ID is not possible because of duplicate ID numbers). | * A valid primary key is a clustered index on the ID field (or ID and ClassTypeID is just the ID is not possible because of duplicate ID numbers). | ||
* This query is automatically run on startup of Chapi starting with version ?.?.? (the version included in Control 5.75). | * This query is automatically run on startup of Chapi starting with version ?.?.? (the version included in Control 5.75). | ||
- | |||
- | |||
This query returns a table with 3 columns listing the tables with missing or invalid primary keys. | This query returns a table with 3 columns listing the tables with missing or invalid primary keys. | ||
+ | |~ Column Name||~ Description|| | ||
+ | |TableName||The name of the table.|| | ||
+ | |Notes||List the reason the table needs correcting. Also, if an error occurs while repairing the primary key, the error text will be included.|| | ||
+ | |HasError||BIT field to indicate if an error has occurred while repairing the primary key. One indicates there was an error. Zero indicates there was no errors.|| | ||
- | + | Risk of Data Corruption if Run Improperly | |
- | ||~ Column Name ||~ Description || | + | |
- | || TableName || The name of the table. || | + | |
- | || Notes || List the reason the table needs correcting. | + | |
- | || HasError || BIT field to indicate if an error has occurred while repairing the primary key. One indicates there was an error. | + | |
- | Risk of Data Corruption if Run Improperly | + | |
**High**. Data is modified in this query. Do not run this except under the direction of a Cyrious Technical Support staff member. Doing otherwise may result in lost or contaminated data. All data modifications done through direct SQL **are permanent and non-reversable**. | **High**. Data is modified in this query. Do not run this except under the direction of a Cyrious Technical Support staff member. Doing otherwise may result in lost or contaminated data. All data modifications done through direct SQL **are permanent and non-reversable**. | ||
+ | SQL | ||
+ | < | ||
+ | code format" | ||
- | SQL | + | – Setting @ApplyChanges to 0 (zero) will just return a report of the tables with invalid or missing primary keys. |
- | + | ||
- | + | ||
- | + | ||
- | [[code_formatsql|code format" | + | |
- | + | ||
- | + | ||
- | + | ||
- | -- Setting @ApplyChanges to 0 (zero) will just return a report of the tables with invalid or missing primary keys. | + | |
- | + | ||
- | + | ||
- | + | ||
- | -- Setting @ApplyChanges to 1 (one) will also alter the tables to correct the primary keys. | + | |
+ | – Setting @ApplyChanges to 1 (one) will also alter the tables to correct the primary keys. | ||
DECLARE @ApplyChanges BIT; | DECLARE @ApplyChanges BIT; | ||
- | + | SET @ApplyChanges 0; | |
- | + | ||
- | SET @ApplyChanges | + | |
- | + | ||
DECLARE @TableName VARCHAR(255); | DECLARE @TableName VARCHAR(255); | ||
- | |||
- | |||
DECLARE @Col_ID INT;; | DECLARE @Col_ID INT;; | ||
- | |||
- | |||
DECLARE @Col_StoreID INT; | DECLARE @Col_StoreID INT; | ||
- | |||
- | |||
DECLARE @Col_ClassTypeID INT; | DECLARE @Col_ClassTypeID INT; | ||
- | |||
- | |||
DECLARE @Col_SeqID INT; | DECLARE @Col_SeqID INT; | ||
- | |||
- | |||
DECLARE @PKFields VARCHAR(MAX); | DECLARE @PKFields VARCHAR(MAX); | ||
- | |||
- | |||
DECLARE @PrimaryKeyIndexName VARCHAR(255); | DECLARE @PrimaryKeyIndexName VARCHAR(255); | ||
- | |||
- | |||
DECLARE @IsPrimaryKeyClustered BIT; | DECLARE @IsPrimaryKeyClustered BIT; | ||
- | |||
- | |||
DECLARE @ClusteredIndexName VARCHAR(255); | DECLARE @ClusteredIndexName VARCHAR(255); | ||
- | |||
- | |||
DECLARE @HasValidPrimaryKey BIT; | DECLARE @HasValidPrimaryKey BIT; | ||
- | |||
- | |||
DECLARE @HasDuplicateIDs BIT; | DECLARE @HasDuplicateIDs BIT; | ||
- | |||
- | |||
DECLARE @HasDuplicateIDClassTypes BIT; | DECLARE @HasDuplicateIDClassTypes BIT; | ||
- | |||
- | |||
DECLARE @Results TABLE(TableName VARCHAR(255), | DECLARE @Results TABLE(TableName VARCHAR(255), | ||
- | |||
- | |||
DECLARE @Notes VARCHAR(MAX); | DECLARE @Notes VARCHAR(MAX); | ||
- | |||
- | |||
DECLARE @HasError BIT; | DECLARE @HasError BIT; | ||
- | |||
- | |||
DECLARE @SQLQuery NVARCHAR(MAX) | DECLARE @SQLQuery NVARCHAR(MAX) | ||
- | |||
- | |||
DECLARE CUR CURSOR FOR | DECLARE CUR CURSOR FOR | ||
+ | SELECT NAME | ||
+ | FROM | ||
- | SELECT NAME | + | WHERE TYPE ' |
- | + | ||
- | + | ||
- | + | ||
- | FROM | + | |
- | + | ||
- | + | ||
- | + | ||
- | | + | |
- | + | ||
- | + | ||
- | + | ||
- | AND SCHEMA_ID | + | |
+ | AND SCHEMA_ID | ||
OPEN CUR | OPEN CUR | ||
- | |||
- | |||
FETCH NEXT FROM CUR INTO @TableName | FETCH NEXT FROM CUR INTO @TableName | ||
- | + | WHILE @@FETCH_STATUS 0 | |
- | + | ||
- | WHILE @@FETCH_STATUS | + | |
- | + | ||
BEGIN | BEGIN | ||
+ | SET @Col_ID | ||
+ | SET @Col_StoreID | ||
- | | + | SET @Col_ClassTypeID |
+ | SET @Col_SeqID | ||
+ | -- Check if it's a standard Control table | ||
+ | IF (@Col_ID | ||
- | SET @Col_StoreID | + | BEGIN |
+ | SET @PKFields | ||
+ | SET @PrimaryKeyIndexName | ||
- | SET @Col_ClassTypeID | + | SET @IsPrimaryKeyClustered |
+ | SET @ClusteredIndexName | ||
+ | SET @HasValidPrimaryKey | ||
- | SET @Col_SeqID | + | SET @HasDuplicateIDs |
- | -- Check if it's a standard Control table | + | |
+ | SET @HasDuplicateIDClassTypes | ||
+ | SELECT @PrimaryKeyIndexName | ||
- | | + | |
+ | WHERE i.is_primary_key | ||
+ | SELECT @ClusteredIndexName | ||
- | | + | |
+ | WHERE i.TYPE | ||
+ | SELECT @PKFields | ||
- | SET @PKFields | + | FROM sys.indexes AS i INNER JOIN |
+ | sys.index_columns AS ic ON i.OBJECT_ID | ||
+ | AND i.index_id | ||
- | SET @PrimaryKeyIndexName | + | WHERE |
+ | ORDER BY ic.index_column_id | ||
+ | IF (@PrimaryKeyIndexName IS NOT NULL) | ||
- | SET @IsPrimaryKeyClustered | + | BEGIN |
+ | IF (@IsPrimaryKeyClustered | ||
+ | SET @HasValidPrimaryKey | ||
- | | + | |
+ | SET @HasValidPrimaryKey | ||
+ | ELSE | ||
- | | + | |
+ | END | ||
+ | ELSE | ||
- | SET @HasDuplicateIDs | + | SET @HasValidPrimaryKey |
+ | IF (@HasValidPrimaryKey | ||
+ | BEGIN | ||
- | SET @HasDuplicateIDClassTypes | + | SET @HasError |
+ | IF (@PrimaryKeyIndexName IS NULL) | ||
+ | SET @Notes | ||
- | | + | ELSE |
+ | BEGIN | ||
+ | SET @Notes | ||
- | FROM | + | IF (@IsPrimaryKeyClustered |
+ | SET @Notes | ||
+ | ELSE | ||
- | WHERE i.is_primary_key | + | SET @Notes |
+ | IF (@ApplyChanges | ||
+ | BEGIN TRY | ||
- | SELECT | + | EXECUTE( 'ALTER TABLE [' + @TableName + '] DROP CONSTRAINT ' + @PrimaryKeyIndexName ); |
+ | END TRY | ||
+ | BEGIN CATCH | ||
- | FROM | + | SET @HasError |
+ | SET @Notes | ||
+ | END CATCH | ||
- | | + | |
- | + | ||
- | + | ||
- | + | ||
- | SELECT @PKFields | + | |
- | + | ||
- | + | ||
- | + | ||
- | FROM sys.indexes AS i INNER JOIN | + | |
- | + | ||
- | + | ||
- | + | ||
- | sys.index_columns AS ic ON i.OBJECT_ID | + | |
- | + | ||
- | + | ||
- | + | ||
- | AND i.index_id | + | |
- | + | ||
- | + | ||
- | + | ||
- | WHERE | + | |
- | + | ||
- | + | ||
- | + | ||
- | ORDER BY ic.index_column_id | + | |
- | + | ||
- | + | ||
- | + | ||
- | IF (@PrimaryKeyIndexName IS NOT NULL) | + | |
+ | IF (@ClusteredIndexName IS NOT NULL) | ||
BEGIN | BEGIN | ||
+ | SET @Notes | ||
+ | IF (@ApplyChanges | ||
- | | + | |
+ | EXECUTE( 'DROP INDEX ' + @ClusteredIndexName + ' ON [' + @TableName + ' | ||
+ | END TRY | ||
- | SET @HasValidPrimaryKey | + | BEGIN CATCH |
+ | SET @HasError | ||
+ | SET @Notes | ||
- | | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @HasValidPrimaryKey | + | |
- | + | ||
- | + | ||
- | + | ||
- | ELSE | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @HasValidPrimaryKey | + | |
- | + | ||
END | END | ||
+ | SET @SQLQuery | ||
+ | @Result | ||
- | ELSE | + | WHEN EXISTS( SELECT |
+ | | ||
+ | GROUP BY ID, ClassTypeID | ||
- | SET @HasValidPrimaryKey | + | |
+ | ELSE 0 | ||
- | IF (@HasValidPrimaryKey | + | END'; |
+ | EXEC sp_executesql @SQLQuery, N' | ||
+ | IF (@HasDuplicateIDClassTypes | ||
BEGIN | BEGIN | ||
+ | SET @HasError | ||
+ | SET @Notes | ||
- | SET @HasError | + | |
- | + | ||
- | + | ||
- | + | ||
- | IF (@PrimaryKeyIndexName IS NULL) | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @Notes | + | |
- | + | ||
- | + | ||
- | + | ||
- | ELSE | + | |
- | + | ||
- | + | ||
- | + | ||
- | BEGIN | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @Notes | + | |
- | + | ||
- | + | ||
- | + | ||
- | IF (@IsPrimaryKeyClustered | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @Notes | + | |
- | + | ||
- | + | ||
- | + | ||
- | ELSE | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @Notes | + | |
- | + | ||
- | + | ||
- | + | ||
- | IF (@ApplyChanges | + | |
- | + | ||
- | + | ||
- | + | ||
- | BEGIN TRY | + | |
- | + | ||
- | + | ||
- | + | ||
- | EXECUTE( 'ALTER TABLE [' + @TableName + '] DROP CONSTRAINT ' + @PrimaryKeyIndexName ); | + | |
- | + | ||
- | + | ||
- | + | ||
- | END TRY | + | |
- | + | ||
- | + | ||
- | + | ||
- | BEGIN CATCH | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @HasError | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @Notes | + | |
- | + | ||
- | + | ||
- | + | ||
- | END CATCH | + | |
- | + | ||
- | + | ||
- | + | ||
- | END; | + | |
- | + | ||
- | + | ||
- | + | ||
- | IF (@ClusteredIndexName IS NOT NULL) | + | |
- | + | ||
- | + | ||
- | + | ||
- | BEGIN | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @Notes | + | |
- | + | ||
- | + | ||
- | + | ||
- | IF (@ApplyChanges | + | |
- | + | ||
- | + | ||
- | + | ||
- | BEGIN TRY | + | |
- | + | ||
- | + | ||
- | + | ||
- | EXECUTE( 'DROP INDEX ' + @ClusteredIndexName + ' ON [' + @TableName + ' | + | |
- | + | ||
- | + | ||
- | + | ||
- | END TRY | + | |
- | + | ||
- | + | ||
- | + | ||
- | BEGIN CATCH | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @HasError | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @Notes | + | |
- | + | ||
- | + | ||
- | + | ||
- | END CATCH | + | |
- | + | ||
- | + | ||
- | + | ||
- | | + | |
+ | ELSE IF (@ApplyChanges | ||
+ | BEGIN TRY | ||
SET @SQLQuery | SET @SQLQuery | ||
- | |||
- | |||
@Result | @Result | ||
- | + | | |
- | + | ||
- | | + | |
- | + | ||
| | ||
+ | GROUP BY ID | ||
- | + | HAVING | |
- | GROUP BY ID, ClassTypeID | + | |
- | + | ||
- | + | ||
- | + | ||
- | HAVING | + | |
- | + | ||
ELSE 0 | ELSE 0 | ||
- | |||
- | |||
END'; | END'; | ||
+ | EXEC sp_executesql @SQLQuery, N' | ||
+ | SET @SQLQuery | ||
- | | + | IF (@HasDuplicateIDs |
- | + | ||
- | + | ||
- | + | ||
- | | + | |
- | + | ||
BEGIN | BEGIN | ||
+ | SET @SQLQuery | ||
- | + | | |
- | SET @HasError | + | |
- | + | ||
- | + | ||
- | + | ||
- | | + | |
- | + | ||
END | END | ||
+ | SET @SQLQuery | ||
+ | EXECUTE(@SQLQuery); | ||
- | ELSE IF (@ApplyChanges | + | END TRY |
+ | BEGIN CATCH | ||
+ | SET @HasError | ||
- | | + | |
+ | END CATCH | ||
+ | INSERT INTO @Results VALUES (@TableName, | ||
- | SET @SQLQuery | + | END |
- | + | ||
- | + | ||
- | + | ||
- | @Result | + | |
- | + | ||
- | + | ||
- | + | ||
- | WHEN EXISTS( SELECT | + | |
- | + | ||
- | + | ||
- | + | ||
- | | + | |
- | + | ||
- | + | ||
- | + | ||
- | GROUP BY ID | + | |
- | + | ||
- | + | ||
- | + | ||
- | | + | |
- | + | ||
- | + | ||
- | + | ||
- | ELSE 0 | + | |
- | + | ||
- | + | ||
- | + | ||
- | END'; | + | |
- | + | ||
- | + | ||
- | + | ||
- | EXEC sp_executesql @SQLQuery, N' | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @SQLQuery | + | |
- | + | ||
- | + | ||
- | + | ||
- | IF (@HasDuplicateIDs | + | |
- | + | ||
- | + | ||
- | + | ||
- | BEGIN | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @SQLQuery | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @Notes | + | |
- | + | ||
- | + | ||
- | + | ||
- | END | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @SQLQuery | + | |
- | + | ||
- | + | ||
- | + | ||
- | EXECUTE(@SQLQuery); | + | |
- | + | ||
- | + | ||
- | + | ||
- | END TRY | + | |
- | + | ||
- | + | ||
- | + | ||
- | BEGIN CATCH | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @HasError | + | |
- | + | ||
- | + | ||
- | + | ||
- | SET @Notes | + | |
- | + | ||
- | + | ||
- | + | ||
- | END CATCH | + | |
- | + | ||
- | + | ||
- | + | ||
- | INSERT INTO @Results VALUES (@TableName, | + | |
- | + | ||
- | + | ||
- | + | ||
- | END | + | |
- | + | ||
- | + | ||
- | + | ||
- | | + | |
- | + | ||
- | + | ||
- | + | ||
- | FETCH NEXT FROM CUR INTO @TableName | + | |
+ | END; | ||
+ | FETCH NEXT FROM CUR INTO @TableName | ||
END | END | ||
- | |||
- | |||
CLOSE CUR | CLOSE CUR | ||
- | |||
- | |||
DEALLOCATE CUR | DEALLOCATE CUR | ||
- | |||
- | |||
SELECT * | SELECT * | ||
+ | FROM @Results | ||
+ | ORDER BY TableName | ||
+ | </ | ||
- | FROM @Results | + | Version Information |
+ | * Entered : 06/02/2016 | ||
+ | Related SQLs | ||
- | ORDER BY TableName | + | '''' |
+ | \\ | ||
- | |||
- | < | ||
- | Version Information | ||
- | * Entered : 06/02/2016 | ||
- | Related SQLs | ||