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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to identify the ID of row which has been modified?

How to identify the ID of row which has been modified?

Scheduled Pinned Locked Moved Database
databasehelptutorialquestionannouncement
4 Posts 4 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.
  • U Offline
    U Offline
    User 4700810
    wrote on last edited by
    #1

    Hi all, I have table Demo:

    CREATE TABLE Demo
    (
    ID int PRIMARY KEY,
    Name char(50),
    BeModified int DEFAULT(0)
    )

    Whenever [Name] is updated, [BeModified] will be set to 1. I think I should create an TRIGGER AFTER UPDATE

    CREATE TRIGGER UpdateRow

    ON Demo

    AFTER UPDATE

    UPDATE Demo

    Set BeModified = 1 WHERE ID = "???"

    My problem is I do not know how to identify the ID of row which has been modified. Have you got any suggestion for me? Thank you so much!

    D M M 3 Replies Last reply
    0
    • U User 4700810

      Hi all, I have table Demo:

      CREATE TABLE Demo
      (
      ID int PRIMARY KEY,
      Name char(50),
      BeModified int DEFAULT(0)
      )

      Whenever [Name] is updated, [BeModified] will be set to 1. I think I should create an TRIGGER AFTER UPDATE

      CREATE TRIGGER UpdateRow

      ON Demo

      AFTER UPDATE

      UPDATE Demo

      Set BeModified = 1 WHERE ID = "???"

      My problem is I do not know how to identify the ID of row which has been modified. Have you got any suggestion for me? Thank you so much!

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      I don't think you really need a trigger here. You must be updating the name through a stored procedure/SQL query. Just make the query update the bool column as well.

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      1 Reply Last reply
      0
      • U User 4700810

        Hi all, I have table Demo:

        CREATE TABLE Demo
        (
        ID int PRIMARY KEY,
        Name char(50),
        BeModified int DEFAULT(0)
        )

        Whenever [Name] is updated, [BeModified] will be set to 1. I think I should create an TRIGGER AFTER UPDATE

        CREATE TRIGGER UpdateRow

        ON Demo

        AFTER UPDATE

        UPDATE Demo

        Set BeModified = 1 WHERE ID = "???"

        My problem is I do not know how to identify the ID of row which has been modified. Have you got any suggestion for me? Thank you so much!

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        As d@nish recommended. There is also a keyword in the trigger that indicates the current record, not sure what it is, I NEVER use triggers, they are evil. As another idea I use a datetime field instead of a boolean, it tells you so much more than a simple yes/no.

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • U User 4700810

          Hi all, I have table Demo:

          CREATE TABLE Demo
          (
          ID int PRIMARY KEY,
          Name char(50),
          BeModified int DEFAULT(0)
          )

          Whenever [Name] is updated, [BeModified] will be set to 1. I think I should create an TRIGGER AFTER UPDATE

          CREATE TRIGGER UpdateRow

          ON Demo

          AFTER UPDATE

          UPDATE Demo

          Set BeModified = 1 WHERE ID = "???"

          My problem is I do not know how to identify the ID of row which has been modified. Have you got any suggestion for me? Thank you so much!

          M Offline
          M Offline
          Member 4501940
          wrote on last edited by
          #4

          I concur. This is NOT the place for a trigger. I only use triggers to store 1)disposable, 2)derived and 3)complex data used for reporting or searching. Must meet all 3 criteria. But to answer the question-

          UPDATE Demo
          Set BeModified = 1 WHERE ID IN (SELECT ID FROM inserted)

          OR

          UPDATE Demo
          Set BeModified = 1
          FROM Demo
          INNER JOIN inserted
          ON Demo.ID = inserted.ID

          This is assuming sql server. The "inserted" table is a virtual table which contains all of the fields and values from the insert, update or delete that fired the trigger. You might think that only a record at a time is updated but in fact since it is possible to update multiple records at the same time, this may NOT be what you want. But thats what you get with triggers. If you ever have to update all of the records in that table, BeModified will be set to 1 for all. Be warned.

          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