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 Trigger not working on update statement

SQL Trigger not working on update statement

Scheduled Pinned Locked Moved Database
databasehelpquestionannouncement
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.
  • B Offline
    B Offline
    BSRK
    wrote on last edited by
    #1

    hi i am having a table with 3 columns (a,b,c). i have written a trigger for INSERT / UPDATE as c=a+b this is working fine as long as i enter/update each row by row manually. but, when i use any update statement for a set of rows, the trigger is firing only for the last row. can anyone please help? Thanks

    E C 2 Replies Last reply
    0
    • B BSRK

      hi i am having a table with 3 columns (a,b,c). i have written a trigger for INSERT / UPDATE as c=a+b this is working fine as long as i enter/update each row by row manually. but, when i use any update statement for a set of rows, the trigger is firing only for the last row. can anyone please help? Thanks

      E Offline
      E Offline
      Eric Dahlvang
      wrote on last edited by
      #2

      If the 'c' column is only a calculation of a+b, why not just use **([a] + [b])** as a formula? Anyway, you could also use INSTEAD OF triggers:

      CREATE TRIGGER InsteadUpdateTrigger on tblTable
      INSTEAD OF UPDATE
      AS
      BEGIN
      UPDATE tblTable
      SET a=i.a,b=i.b,c=i.a+i.b
      FROM INSERTED i
      WHERE tblTable.IDField = i.IDField
      END

      CREATE TRIGGER InsteadInsertTrigger on tblTable
      INSTEAD OF INSERT
      AS
      BEGIN
      INSERT INTO tblTable
      SELECT a,b,a+b
      FROM INSERTED
      END

      --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

      1 Reply Last reply
      0
      • B BSRK

        hi i am having a table with 3 columns (a,b,c). i have written a trigger for INSERT / UPDATE as c=a+b this is working fine as long as i enter/update each row by row manually. but, when i use any update statement for a set of rows, the trigger is firing only for the last row. can anyone please help? Thanks

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

        Is there a reason for not using a VIEW on this table? The table would have columns a and b. The view would have columns a, b, and c where c is defined as (a+b).

        Chris Meech I am Canadian. [heard in a local bar] Nobody likes jerks. [espeir] The zen of the soapbox is hard to attain...[Jörgen Sigvardsson] I wish I could remember what it was like to only have a short term memory.[David Kentley]

        E 1 Reply Last reply
        0
        • C Chris Meech

          Is there a reason for not using a VIEW on this table? The table would have columns a and b. The view would have columns a, b, and c where c is defined as (a+b).

          Chris Meech I am Canadian. [heard in a local bar] Nobody likes jerks. [espeir] The zen of the soapbox is hard to attain...[Jörgen Sigvardsson] I wish I could remember what it was like to only have a short term memory.[David Kentley]

          E Offline
          E Offline
          Eric Dahlvang
          wrote on last edited by
          #4

          Or, just give the c field a formula of ([a] + [b]):

          CREATE TABLE [tblTable] (
          [IDField] [int] IDENTITY (1, 1) NOT NULL ,
          [a] [int] NULL ,
          [b] [int] NULL ,
          **[c] AS ([a] + [b])** ,
          CONSTRAINT [PK_tblIDField] PRIMARY KEY CLUSTERED
          (
          [IDField]
          ) ON [PRIMARY]
          ) ON [PRIMARY]

          --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

          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