1

How to create a automatically generated timestamp column in table in Microsoft SQL Server 2019? Timestamp column should be automatically generated when I insert or update table.

In IBM Db2 database the syntax is the following:

    create table myschema.mytable (col1 int, col2 timestamp not null generated always for each row on update as row change timestamp
    insert into myschema.mytable (col1) values (1)
    update myschema.mytable set col1 = 2

After insert/update of column col1, column col2 is automatically generated as current timestamp.

7
  • 2
    Assuming by timestamp you mean the current datetime, then mycolumn datetime not null default(current_timestamp)
    – Dale K
    Commented Jul 15, 2020 at 7:29
  • 1
    If you mean a version field have a look at rowversion this is updated on every row change, but doesn't represent an actual date Commented Jul 15, 2020 at 7:30
  • 2
    If you need an actual datetime column (as opposed to a rowversion) you either need a trigger, or to run your update through a SP which adds it. There is no automated way other than rowversion.
    – Dale K
    Commented Jul 15, 2020 at 7:57
  • I need to update timestamp column (datetime in MS-SQL language) also after every UPDATE statement. I know I can write trigger, but I would like to avoid additional complexity if possible.
    – folow
    Commented Jul 15, 2020 at 8:56
  • 2
    Sorry, there is no automated way - trigger or SP.
    – Dale K
    Commented Jul 15, 2020 at 9:24

1 Answer 1

1

In Microsoft SQL Server you can try this code:

CREATE TABLE myschema.mytable
(
    col1 int, 
    col2 datetime not null default(current_timestamp)
)

INSERT INTO myschema.mytable(col1) VALUES (1)

UPDATE myschema.mytable SET col1 = 2

SELECT * FROM myschema.mytable

Update: Let's create temporary table for test

DECLARE @mytable TABLE
(
    col1 int, 
    col2 datetime not null default(current_timestamp)
)

INSERT INTO @mytable(col1) VALUES (1)
SELECT * FROM @mytable

UPDATE @mytable SET col1 = 2
SELECT * FROM @mytable
16
  • 1
    @DaleK did not want to assign your comment in your answer, sorry! I hope I didn’t offend you. What should I do? Commented Jul 15, 2020 at 7:44
  • 2
    Keep in mind a default doesn't update on updates that is requested in the question Commented Jul 15, 2020 at 7:44
  • 1
    Ar yes, OP wants the timestamp column to update on update (as well as insert).
    – Dale K
    Commented Jul 15, 2020 at 7:55
  • 1
    Yes, why I copied the version of @DaleK to my answer. current_timestamp is ANSI, same as in question. stackoverflow.com/a/24226775/5309660 Commented Jul 15, 2020 at 7:57
  • 1
    @timnavigate, in your example col2 timestamp field is only generated after INSERT, but I also need to change col2 timestamp field on every UPDATE. To simplify for every INSERT or UPDATE timestamp column col2 should be changed.
    – folow
    Commented Jul 15, 2020 at 8:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.