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. Create a trigger

Create a trigger

Scheduled Pinned Locked Moved Database
databasehelptutorialquestionannouncement
5 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.
  • L Offline
    L Offline
    lionelcyril
    wrote on last edited by
    #1

    I wanted to create a trigger on a table. Whenever an update statement gets executed on the table, the trigger will be fired and a column “UPDATED_DT” of the table will be updated with the current date. i have used the following but it has compilation errors cause with after update i cannot use the new keyword as it is technically incorrect. If works fine when i use BEFORE UPDATE. Could you guide me with this? CREATE OR REPLACE TRIGGER TRG_AU AFTER UPDATE ON X_TABLE FOR EACH ROW BEGIN :NEW.UPDATED_DT := SYSDATE; --error with the new keyboard any other approach END TRG_AU Can someone advice a solution?

    W L 2 Replies Last reply
    0
    • L lionelcyril

      I wanted to create a trigger on a table. Whenever an update statement gets executed on the table, the trigger will be fired and a column “UPDATED_DT” of the table will be updated with the current date. i have used the following but it has compilation errors cause with after update i cannot use the new keyword as it is technically incorrect. If works fine when i use BEFORE UPDATE. Could you guide me with this? CREATE OR REPLACE TRIGGER TRG_AU AFTER UPDATE ON X_TABLE FOR EACH ROW BEGIN :NEW.UPDATED_DT := SYSDATE; --error with the new keyboard any other approach END TRG_AU Can someone advice a solution?

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      What's the reason you cannot use BEFORE trigger? Before trigger is the only version where you can modify the values to be written to the row.

      The need to optimize rises from a bad design.My articles[^]

      L 1 Reply Last reply
      0
      • W Wendelius

        What's the reason you cannot use BEFORE trigger? Before trigger is the only version where you can modify the values to be written to the row.

        The need to optimize rises from a bad design.My articles[^]

        L Offline
        L Offline
        lionelcyril
        wrote on last edited by
        #3

        I have been asked to do so.

        W 1 Reply Last reply
        0
        • L lionelcyril

          I have been asked to do so.

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Quite odd. If you're asked to modify the values of an updating row inside a trigger, this cannot be done in after trigger. You could possibly update the same row again in the after trigger BUT that would be a recursion and could run until you run out of memory... If the goal is to have the latest update timestamp on every row (implemented using a trigger), I would re-check why before trigger isn't an option.

          The need to optimize rises from a bad design.My articles[^]

          1 Reply Last reply
          0
          • L lionelcyril

            I wanted to create a trigger on a table. Whenever an update statement gets executed on the table, the trigger will be fired and a column “UPDATED_DT” of the table will be updated with the current date. i have used the following but it has compilation errors cause with after update i cannot use the new keyword as it is technically incorrect. If works fine when i use BEFORE UPDATE. Could you guide me with this? CREATE OR REPLACE TRIGGER TRG_AU AFTER UPDATE ON X_TABLE FOR EACH ROW BEGIN :NEW.UPDATED_DT := SYSDATE; --error with the new keyboard any other approach END TRG_AU Can someone advice a solution?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            The only way you can change the values of a row being updated is in a BEFORE UPDATE trigger. AFTER UPDATE trigger is triggered "after" the row is updated, and therefore, understandably, the :NEW table can't be modified.

            "Don't confuse experts with facts" - Eric_V

            modified on Friday, August 19, 2011 10:34 AM

            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