Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. MS SQL Server Trigger help

MS SQL Server Trigger help

Scheduled Pinned Locked Moved Database
databasehelpsql-serversysadminannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eddjusted
    wrote on last edited by
    #1

    Hi, I have a sql server table with fields EmployeeID, Date, TimeIN, TimeOut, Remarks. Now there is a unique constraint on both EmployeeID and Date such that only one record exists for a particular Employee on a particular date. Now the main application can insert and update records. I need a trigger to track changes such that on insert or update, the values are copied onto a data_log table. Here is the problem, I need the trigger to check if the row exists in data_log for that EmployeeID and Date, then update, else insert. So there should be only one record of each employee per date in the data_log table as well. I just want the most current change and need to overwrite previous log entry. thanks

    R C 2 Replies Last reply
    0
    • E eddjusted

      Hi, I have a sql server table with fields EmployeeID, Date, TimeIN, TimeOut, Remarks. Now there is a unique constraint on both EmployeeID and Date such that only one record exists for a particular Employee on a particular date. Now the main application can insert and update records. I need a trigger to track changes such that on insert or update, the values are copied onto a data_log table. Here is the problem, I need the trigger to check if the row exists in data_log for that EmployeeID and Date, then update, else insert. So there should be only one record of each employee per date in the data_log table as well. I just want the most current change and need to overwrite previous log entry. thanks

      R Offline
      R Offline
      R Giskard Reventlov
      wrote on last edited by
      #2

      Just a thought... why are you bothering if all you are doing is copying the values into another table? An audit or log table should contain complete history, I've always thought and no unique keys from the parent table - duplicates are fine - create a compound index or a surrogate key if you need one. Prepared to be called out on this - just seems a bit pointless. In the meantime you could check with:

      if exists(select top 1 1 from [table] where [key] = [parameter])
      begin
      -- insert
      end
      else
      begin
      -- update
      end

      or similar: adjust to suit.

      "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

      E 1 Reply Last reply
      0
      • R R Giskard Reventlov

        Just a thought... why are you bothering if all you are doing is copying the values into another table? An audit or log table should contain complete history, I've always thought and no unique keys from the parent table - duplicates are fine - create a compound index or a surrogate key if you need one. Prepared to be called out on this - just seems a bit pointless. In the meantime you could check with:

        if exists(select top 1 1 from [table] where [key] = [parameter])
        begin
        -- insert
        end
        else
        begin
        -- update
        end

        or similar: adjust to suit.

        "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

        E Offline
        E Offline
        eddjusted
        wrote on last edited by
        #3

        I have to run extended operations on bulk records in the destination table. I thought Delete if exists, then Insert would be a possible solution, but the trigger is working some times and failing other times, and when it does fail, it cancels out the transaction on the original table resulting in data loss.

        1 Reply Last reply
        0
        • E eddjusted

          Hi, I have a sql server table with fields EmployeeID, Date, TimeIN, TimeOut, Remarks. Now there is a unique constraint on both EmployeeID and Date such that only one record exists for a particular Employee on a particular date. Now the main application can insert and update records. I need a trigger to track changes such that on insert or update, the values are copied onto a data_log table. Here is the problem, I need the trigger to check if the row exists in data_log for that EmployeeID and Date, then update, else insert. So there should be only one record of each employee per date in the data_log table as well. I just want the most current change and need to overwrite previous log entry. thanks

          C Offline
          C Offline
          Corporal Agarn
          wrote on last edited by
          #4

          I do not know which version of SQL server you have but if 2008 try MERGE in your trigger.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups