Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009

    Unanswered: Unique Records Insert

    Any help appreciated.

    I'm creating a helpdesk solution where i am extracting emails through service from MS Exchange into SQL using Exchange API. The following query works fine and in SQL Server 2012, i have the MessageID column set to unique, indexed and ignore duplicate keys set to yes.

    cmd.CommandText = "INSERT INTO Tickets ([Oid], [MessageID]) VALUES (@GuidValue, LEFT('" & E_ID & "', 255))"

    The service grabs the most recent 10 emails and pops them into SQL

    i've created a trigger on the table Tickets to insert a value of 1 in a column called InsertFlag for new records inserted through the service

    the windows service obtains the 10 most recent emails every 10 minutes and inserts them in an SQL Table called Tickets
    let's say on the next windows service run, there are only 2 new emails
    the trigger still seems to update the other 8 existing records also


    USE Helpdesk
    CREATE TRIGGER InsertFlagTrigger
    ON dbo.Tickets
    DECLARE @insertflag int
    Set @insertflag = '1'
    update Tickets
    set InsertedFlag = @insertflag

    Is it the Trigger causing the issue or is SQL ignoring duplicate keys set to yes and still updating the 10 records if only 2 new records are new ?

    Many thanks

  2. #2
    Join Date
    Jan 2007
    Provided Answers: 12
    Your trigger code applies to everything in the transaction. Basically: "For all the rows you've just inserted, set the inserted flag = 1"

    Because you're ignoring the unique constraint, you are allowing all 10 records, including the 8 duplicates, to be created. You need to change your code to only insert those that do not already exist.

    Something like:
    INSERT INTO dbo.Tickets (list, of, columns)
    SELECT list
         , of
         , columns
    FROM   source_table
            SELECT *
            FROM   dbo.Tickets
            WHERE  MessageID = source_table.MessageID
    This will only insert records that do not already exist (based on MessageID).

    Also - I don't understand why you need that trigger...
    Home | Blog

  3. #3
    Join Date
    Oct 2009
    Thanks, much appreciated. The problem is that i don't know the source_table name etc. It is a straight insert into the database as i get the info from accessing the Microsoft Exchange API

    INSERT INTO Tickets ([Oid], [MessageID]) VALUES (@GuidValue, LEFT('" & E_ID & "', 255))"

    I'm trying to implement the trigger so i can mark the inserts already inserted just for information

  4. #4
    Join Date
    Nov 2004
    on the wrong server
    Provided Answers: 6
    Why would you ignore duplicate keys? Your trigger is no good. It is going to update every record in the table every time regardless of how it was inserted. You need to join to the INSERTED virtual table on the primary key and add some logic for determining how the record was inserted. I am not even sure how you would do that from a Trigger because SELECT OBJECT_NAME(@@PROCID) will just return the trigger name I think. Also in GVEEs insert statement I would consider using distinct if the records in the source table are not guaranteed distinct.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts