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. SQL Server Trigger [modified]

SQL Server Trigger [modified]

Scheduled Pinned Locked Moved Database
databasequestionsql-serverwcfsysadmin
4 Posts 2 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.
  • J Offline
    J Offline
    Jay Royall
    wrote on last edited by
    #1

    Hi, Scenario: I have a database which is going to be replicated via a set of web services. The web services will select all records where [AwaitingSync] = true. I have cerated two triggers for each table which sets [AwaitingSync] to true when a record is inserted or updated. The web service then sets [AwaitingSync] to false for all records. Problem: Due to a combination of my inexperience and lack of thought there is a slight problem(you'ver probably already noticed what the problem is!). After synching, when the web service sets [AwaitingSync] to false the update trigger fires and sets it back to true! :doh: My question is this then; what can I do about this? Can I add a conditional statement to the trigger so that it only fires if a particular column is being updated(or not being updated in this case). Or maybe the trigger be temporarily disabled while the [AwaitingSync] column is being updated. Or are there any other solutions to this? Thanks for your time. EDIT: Think I've found a solution but I would still appreciate your views if there is a better way of doing this. I have modified the trigger to check the value of [AwaitingSync] before updating.

    IF(SELECT [AwaitingSync] FROM DELETED) = 0
    BEGIN
    UPDATE tblProductCategories
    SET [DateModified] = GetDate(),
    [AwaitingSync] = 1
    WHERE [ID] = (SELECT [ID] FROM INSERTED)
    END

    modified on Monday, October 25, 2010 11:21 AM

    C 1 Reply Last reply
    0
    • J Jay Royall

      Hi, Scenario: I have a database which is going to be replicated via a set of web services. The web services will select all records where [AwaitingSync] = true. I have cerated two triggers for each table which sets [AwaitingSync] to true when a record is inserted or updated. The web service then sets [AwaitingSync] to false for all records. Problem: Due to a combination of my inexperience and lack of thought there is a slight problem(you'ver probably already noticed what the problem is!). After synching, when the web service sets [AwaitingSync] to false the update trigger fires and sets it back to true! :doh: My question is this then; what can I do about this? Can I add a conditional statement to the trigger so that it only fires if a particular column is being updated(or not being updated in this case). Or maybe the trigger be temporarily disabled while the [AwaitingSync] column is being updated. Or are there any other solutions to this? Thanks for your time. EDIT: Think I've found a solution but I would still appreciate your views if there is a better way of doing this. I have modified the trigger to check the value of [AwaitingSync] before updating.

      IF(SELECT [AwaitingSync] FROM DELETED) = 0
      BEGIN
      UPDATE tblProductCategories
      SET [DateModified] = GetDate(),
      [AwaitingSync] = 1
      WHERE [ID] = (SELECT [ID] FROM INSERTED)
      END

      modified on Monday, October 25, 2010 11:21 AM

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      Your update trigger should exclude the column [Awaiting Synch] from the list of columns that the trigger should fire on. :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

      J 1 Reply Last reply
      0
      • C Chris Meech

        Your update trigger should exclude the column [Awaiting Synch] from the list of columns that the trigger should fire on. :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

        J Offline
        J Offline
        Jay Royall
        wrote on last edited by
        #3

        Ah! I didn't realise that you could specify what columns fired the trigger. This is the current trigger definition:

        CREATE TRIGGER [dbo].[trgProductCategoriesUPDATE]
        ON [dbo].[tblProductCategories]
        AFTER UPDATE
        AS
        BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;

        UPDATE	tblProductCategories
        SET	\[DateModified\] = GetDate(),
        	\[AwaitingSync\] = 1
        WHERE	\[ID\] = (SELECT \[ID\] FROM INSERTED)
        

        END

        Which fires when any column is updated. Are you saying that it should look something like this:

        CREATE TRIGGER [dbo].[trgProductCategoriesUPDATE]
        ON [dbo].[tblProductCategories]
        AFTER UPDATE
        AS
        BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;

            IF UPDATE(CategoryName)
            BEGIN
                UPDATE  tblProductCategories
                SET     \[DateModified\] = GetDate(),
        	        \[AwaitingSync\] = 1
                WHERE	\[ID\] = (SELECT \[ID\] FROM INSERTED)
            END
        

        END

        Thanks :)

        C 1 Reply Last reply
        0
        • J Jay Royall

          Ah! I didn't realise that you could specify what columns fired the trigger. This is the current trigger definition:

          CREATE TRIGGER [dbo].[trgProductCategoriesUPDATE]
          ON [dbo].[tblProductCategories]
          AFTER UPDATE
          AS
          BEGIN
          -- SET NOCOUNT ON added to prevent extra result sets from
          -- interfering with SELECT statements.
          SET NOCOUNT ON;

          UPDATE	tblProductCategories
          SET	\[DateModified\] = GetDate(),
          	\[AwaitingSync\] = 1
          WHERE	\[ID\] = (SELECT \[ID\] FROM INSERTED)
          

          END

          Which fires when any column is updated. Are you saying that it should look something like this:

          CREATE TRIGGER [dbo].[trgProductCategoriesUPDATE]
          ON [dbo].[tblProductCategories]
          AFTER UPDATE
          AS
          BEGIN
          -- SET NOCOUNT ON added to prevent extra result sets from
          -- interfering with SELECT statements.
          SET NOCOUNT ON;

              IF UPDATE(CategoryName)
              BEGIN
                  UPDATE  tblProductCategories
                  SET     \[DateModified\] = GetDate(),
          	        \[AwaitingSync\] = 1
                  WHERE	\[ID\] = (SELECT \[ID\] FROM INSERTED)
              END
          

          END

          Thanks :)

          C Offline
          C Offline
          Chris Meech
          wrote on last edited by
          #4

          Yes that is correct. I'm not familiar with the SQL Server flavour of doing things, but when defining a trigger you can specify a list of columns as opposed to the entire table, that the trigger is fired on. Glad that has helped you out. :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

          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