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. General Programming
  3. C#
  4. Auto-increment value on UPDATE

Auto-increment value on UPDATE

Scheduled Pinned Locked Moved C#
databaseannouncementquestion
8 Posts 5 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.
  • D Offline
    D Offline
    Dewald
    wrote on last edited by
    #1

    Do you guys know of any easy way to have one column in a table of which the value auto updates every time an UPDATE statement is called for a specific row? Either one of the following two scenarios would be fine but I'm not sure what the easiest way would be of implementing it: 1. Let's say there's a column in the table called 'Version' (int). Every time an UPDATE statement is called, the value of the 'Version' column is automatically incremented by 1 for every row affected. 2. Let's say there's a column in the table called 'Modified' (datetime). Every time an UPDATE statement is called, the value of the 'Modified' column is automatically set to the current date and time. I have a similar concept to this in my DB with a column named 'Inserted' which has a default value of GetDate(). So for every INSERT into the table, the 'Inserted' column will automatically get the current date and time but I'd like to have something like this for UPDATE as well. Any ideas? My best idea so far would be to create a trigger on the table, something as follows:

    CREATE TRIGGER update_mytable
    ON MyTable
    FOR UPDATE
    AS
    BEGIN
    UPDATE MyTable
    SET Modified = GETDATE()
    WHERE RecordID IN (SELECT RecordID FROM INSERTED)
    END

    But I'm hoping that there might be a more elegant solution.

    P L A D 4 Replies Last reply
    0
    • D Dewald

      Do you guys know of any easy way to have one column in a table of which the value auto updates every time an UPDATE statement is called for a specific row? Either one of the following two scenarios would be fine but I'm not sure what the easiest way would be of implementing it: 1. Let's say there's a column in the table called 'Version' (int). Every time an UPDATE statement is called, the value of the 'Version' column is automatically incremented by 1 for every row affected. 2. Let's say there's a column in the table called 'Modified' (datetime). Every time an UPDATE statement is called, the value of the 'Modified' column is automatically set to the current date and time. I have a similar concept to this in my DB with a column named 'Inserted' which has a default value of GetDate(). So for every INSERT into the table, the 'Inserted' column will automatically get the current date and time but I'd like to have something like this for UPDATE as well. Any ideas? My best idea so far would be to create a trigger on the table, something as follows:

      CREATE TRIGGER update_mytable
      ON MyTable
      FOR UPDATE
      AS
      BEGIN
      UPDATE MyTable
      SET Modified = GETDATE()
      WHERE RecordID IN (SELECT RecordID FROM INSERTED)
      END

      But I'm hoping that there might be a more elegant solution.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      This is an elegant solution, and is definitely your best bet here. Note that you should do an = test, not an IN test.

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      D 1 Reply Last reply
      0
      • D Dewald

        Do you guys know of any easy way to have one column in a table of which the value auto updates every time an UPDATE statement is called for a specific row? Either one of the following two scenarios would be fine but I'm not sure what the easiest way would be of implementing it: 1. Let's say there's a column in the table called 'Version' (int). Every time an UPDATE statement is called, the value of the 'Version' column is automatically incremented by 1 for every row affected. 2. Let's say there's a column in the table called 'Modified' (datetime). Every time an UPDATE statement is called, the value of the 'Modified' column is automatically set to the current date and time. I have a similar concept to this in my DB with a column named 'Inserted' which has a default value of GetDate(). So for every INSERT into the table, the 'Inserted' column will automatically get the current date and time but I'd like to have something like this for UPDATE as well. Any ideas? My best idea so far would be to create a trigger on the table, something as follows:

        CREATE TRIGGER update_mytable
        ON MyTable
        FOR UPDATE
        AS
        BEGIN
        UPDATE MyTable
        SET Modified = GETDATE()
        WHERE RecordID IN (SELECT RecordID FROM INSERTED)
        END

        But I'm hoping that there might be a more elegant solution.

        L Offline
        L Offline
        Lukasz Nowakowski
        wrote on last edited by
        #3

        I think that's the best approach. It should work for all updates. Second way I can think of is to create procedure for updating the table and using it for all updates, that occur. And inside this procedure you can also update your version and modified columns. But that required you to stick with the procedures for all updates.

        Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

        1 Reply Last reply
        0
        • P Pete OHanlon

          This is an elegant solution, and is definitely your best bet here. Note that you should do an = test, not an IN test.

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          D Offline
          D Offline
          Dewald
          wrote on last edited by
          #4

          Thanks. But why would you say that I should use an = test as opposed to an IN test? What if the UPDATE statement affects more than one row? Is there any specific disadvantage to usign an IN test that I should consider?

          1 Reply Last reply
          0
          • D Dewald

            Do you guys know of any easy way to have one column in a table of which the value auto updates every time an UPDATE statement is called for a specific row? Either one of the following two scenarios would be fine but I'm not sure what the easiest way would be of implementing it: 1. Let's say there's a column in the table called 'Version' (int). Every time an UPDATE statement is called, the value of the 'Version' column is automatically incremented by 1 for every row affected. 2. Let's say there's a column in the table called 'Modified' (datetime). Every time an UPDATE statement is called, the value of the 'Modified' column is automatically set to the current date and time. I have a similar concept to this in my DB with a column named 'Inserted' which has a default value of GetDate(). So for every INSERT into the table, the 'Inserted' column will automatically get the current date and time but I'd like to have something like this for UPDATE as well. Any ideas? My best idea so far would be to create a trigger on the table, something as follows:

            CREATE TRIGGER update_mytable
            ON MyTable
            FOR UPDATE
            AS
            BEGIN
            UPDATE MyTable
            SET Modified = GETDATE()
            WHERE RecordID IN (SELECT RecordID FROM INSERTED)
            END

            But I'm hoping that there might be a more elegant solution.

            A Offline
            A Offline
            Abhinav S
            wrote on last edited by
            #5

            Looks to be a good solution in my opinion. Just consider using an INNER JOIN instead of IN UPDATE mytable Set Modified = GetDate() From myTable A Inner Join Inserted B Where A.RecordId = B.RecordId

            D 1 Reply Last reply
            0
            • A Abhinav S

              Looks to be a good solution in my opinion. Just consider using an INNER JOIN instead of IN UPDATE mytable Set Modified = GetDate() From myTable A Inner Join Inserted B Where A.RecordId = B.RecordId

              D Offline
              D Offline
              Dewald
              wrote on last edited by
              #6

              Thanks. Would you mind explaining the advantage of an INNER JOIN over an IN?

              A 1 Reply Last reply
              0
              • D Dewald

                Do you guys know of any easy way to have one column in a table of which the value auto updates every time an UPDATE statement is called for a specific row? Either one of the following two scenarios would be fine but I'm not sure what the easiest way would be of implementing it: 1. Let's say there's a column in the table called 'Version' (int). Every time an UPDATE statement is called, the value of the 'Version' column is automatically incremented by 1 for every row affected. 2. Let's say there's a column in the table called 'Modified' (datetime). Every time an UPDATE statement is called, the value of the 'Modified' column is automatically set to the current date and time. I have a similar concept to this in my DB with a column named 'Inserted' which has a default value of GetDate(). So for every INSERT into the table, the 'Inserted' column will automatically get the current date and time but I'd like to have something like this for UPDATE as well. Any ideas? My best idea so far would be to create a trigger on the table, something as follows:

                CREATE TRIGGER update_mytable
                ON MyTable
                FOR UPDATE
                AS
                BEGIN
                UPDATE MyTable
                SET Modified = GETDATE()
                WHERE RecordID IN (SELECT RecordID FROM INSERTED)
                END

                But I'm hoping that there might be a more elegant solution.

                D Offline
                D Offline
                David Skelly
                wrote on last edited by
                #7

                One thing to bear in mind: GETDATE isn't particularly accurate at the millisecond level. Although the datetime datatype is accurate to about 3 milliseconds, GETDATE does not give you that level of precision, as far as I can gather from a quick google it is somewhere around 15ms although things like machine load can affect it. If you are expecting to do repeated updates to the same row within a very short time period, this may not give you unique values for your Modified column. If your rows will not be updated that frequently, then it's not a problem. I thought SQL Server supported the ability to automatically timestamp rows when they were updated. The timestamp is just a meaningless number but at least you can check whether a row is the version you expect it to be, and you can also tell the order in which updates were applied. (I'm assuming SQL Server since you haven't said what database you are using.)

                1 Reply Last reply
                0
                • D Dewald

                  Thanks. Would you mind explaining the advantage of an INNER JOIN over an IN?

                  A Offline
                  A Offline
                  Abhinav S
                  wrote on last edited by
                  #8

                  Sub queries are run in a temporary memory space, hence they can be slower. However, most engines now are capable of converting sub queries into INs, so in general for new databases, you should be ok with both.

                  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