0

I am getting the following error

The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.MY_TABL' and the index name 'idxReqId'. The duplicate key value is (0).

The script alter table, add columns and finally add a unique nonclustered index. The script fails on this line

CREATE UNIQUE NONCLUSTERED INDEX idxReqId ON [dbo].[MY_TABL](MY_REQID)

I did the following query:

SELECT * FROM sys.indexes WHERE name='idxReqId' AND object_id = OBJECT_ID('[dbo].[MY_TABL]')

I got nothing back

I did a query for SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID('[dbo].[MY_TABL]') and got 1 returned but it is for a CLUSTERED index.

I can't figure how to get past this error. Any help is appreciated.

USE [TestWebDb2_Local]
GO



 SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

SET XACT_ABORT ON
GO

BEGIN TRANSACTION

BEGIN TRY 

    IF COLUMNPROPERTY(OBJECT_ID('[dbo].[MY_TABL]'), 'MY_REQID', 'ColumnId') IS NULL
    BEGIN
        ALTER TABLE [dbo].[MY_TABL] ADD MY_REQID INT NOT NULL DEFAULT(0) 
    END

    IF COLUMNPROPERTY(OBJECT_ID('dbo.MY_TABL'), 'MY_ISCOMPLETED', 'ColumnId') IS NULL
    BEGIN
        ALTER TABLE [dbo].[MY_TABL] ADD MY_ISCOMPLETED BIT NOT NULL DEFAULT(0)
    END
END TRY
BEGIN CATCH
        -- Test whether the transaction is uncommittable.
        IF (XACT_STATE()) = -1
        BEGIN

            PRINT 'The transaction from Alter table is in an uncommittable state.' +
                  ' Rolling back transaction.'
            ROLLBACK TRANSACTION;
        END;
END CATCH;
GO 


 BEGIN TRY 
    UPDATE [dbo].[MY_TABL] SET MY_REQID = 20 WHERE MY_SHORT_NAME = '1035'
    UPDATE [dbo].[MY_TABL] SET MY_REQID = 37, MY_ISCOMPLETED = 1 WHERE MY_SHORT_NAME = 'ABVRPARA'

END TRY
BEGIN CATCH
        IF (XACT_STATE()) = -1
        BEGIN
            PRINT 'The transaction from UPDATE table is in an uncommittable state.' +
                  ' Rolling back transaction.'
            ROLLBACK TRANSACTION;
        END;
END CATCH;


BEGIN TRY
        CREATE UNIQUE NONCLUSTERED INDEX idxReqId ON [dbo].[MY_TABL](MY_REQID)
        PRINT 'Index created'
END TRY
BEGIN CATCH
        -- Test whether the transaction is uncommittable.
        IF (XACT_STATE()) = -1
        BEGIN
            DECLARE @ErrorMessage NVARCHAR(4000);
            DECLARE @ErrorSeverity INT;
            DECLARE @ErrorState INT;

            SELECT 
                @ErrorMessage = ERROR_MESSAGE(),
                @ErrorSeverity = ERROR_SEVERITY(),
                @ErrorState = ERROR_STATE();

            -- Use RAISERROR inside the CATCH block to return error
            -- information about the original error that caused
            -- execution to jump to the CATCH block.
            RAISERROR (@ErrorMessage, -- Message text.
                       @ErrorSeverity, -- Severity.
                       @ErrorState -- State.
                       );


            PRINT 'The transaction from Create Index is in an uncommittable state.' +
                  ' Rolling back transaction.'
            ROLLBACK TRANSACTION;
        END;
END CATCH;
GO

if XACT_STATE() = 1
BEGIN
    COMMIT TRANSACTION  
    PRINT 'Transaction committed.';
END;  

1 Answer 1

1

Run the following query

SELECT MY_REQID, COUNT(*) as COUNT
FROM [dbo].[MY_TABL]
GROUP BY MY_REQID
HAVING COUNT(*) > 1

This will give you a list of MY_REQIDs that are not UNIQUE. To make this index they must be unique. Delete the duplicates / Figure out why they are not unique now that you know which / how many records are not unique.

Sign up to request clarification or add additional context in comments.

4 Comments

Since the script failed on the Create Index, it never adds the MY_REQID column so I am getting 'Invalid column name 'MY_REQID'
That is not what I see... what I see is an ALTER table that adds this column with a default of 0 -- every row in the table will have the same value for something you want to create a unique index on. Feel free to tell me I'm wrong -- or you could try running the SQL I posted and learning something.
Run the script and did a select and no new columns appears
Are you sure you did a COMMIT TRANSACTION

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.