SQL Server Performance

How to pick up duplicates in one column

Discussion in 'SQL Server 2008 General Developer Questions' started by JR3, Aug 11, 2014.

  1. JR3 New Member

    Hi There

    I am new at SQL, im trying to pick up duplicate entries in a column in one table using a trigger.
    I have to use the trigger. I want help in trying to search for duplicate values in the column before inserting another but i just cant get it to work how i want it. What am i doing wrong?

    here is my trigger:

    ALTER TRIGGER DUPLICATES
    ON AMGR_User_Fields_Tbl

    AFTER INSERT,
    UPDATE
    AS

    Declare @Counter integer
    Declare @Value integer

    set @Counter = (Select top 1 AlphanumericCol from AMGR_User_Fields_Tbl order by AlphanumericCol desc) -- this will bring back the last record
    set @Value = @Counter + 1


    IF EXISTS(select top 1 * from AMGR_User_Fields_Tbl order by AlphanumericCol desc)
    BEGIN
    RAISERROR('This row already exists in the table', 16, 1)
    ROLLBACK TRAN
    END;

Share This Page