Purpose to use timestamp datatype in sql server?

JJ TT 141 Reputation points
2021-01-20T22:14:32.32+00:00

Hello,

  1. What is the purpose to use timestamp datatype in sql server?
  2. Do you have a concrete example of using timestamp in real business case?
  3. When is it relevant to use timestampe datatype?

Thank you!

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,367 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 51,346 Reputation points
    2021-01-20T22:53:20.617+00:00
    1. None. It is deprecated and should not be used in code. ROWVERSION is the correct type to use for cases where TIMESTAMP was used before. If you need a time then use one of the time data types.
    2. There is no valid use for this anymore. It is deprecated. Historically it might have been used for getting a somewhat unique value.
    3. Never. It is deprecated.
    0 comments No comments

  2. Erland Sommarskog 107.2K Reputation points
    2021-01-20T22:57:08.553+00:00

    This datatype is also known as "rowversion", and the original name is really unfortunate.

    A timestamp column is automatically updated whenever a row is updated, and the value is monotonically increasing in the database. There is absolutely no relation to date and time.

    The main purpose of timestamp is to implement optimistic concurrency. The client reads a row from the database gets the timestamp value from the database and sends it back on UPDATE. The UPDATE operation includes a condition on timestamp in the WHERE clause, and if the timestamp value has changed since the client read the value, the update is not performed - and the update that came in-between is not lost.

    timestamp/rowversion does not seem to be that popular these days, but I have worked with a system where have many of these columns for exactly this reason.

    Because it is monotonically increasing, some people also use it to find the most recently changed rows in a table, but this is not what it is designed for, and it does not work well in high-concurrency environment. SQL Server offers better features for this purpose, for instance Change Tracking.

    0 comments No comments

  3. CathyJi-MSFT 21,136 Reputation points Microsoft Vendor
    2021-01-21T03:08:06.77+00:00

    Hi @JJ TT ,

    >What is the purpose to use timestamp datatype in sql server?

    According to MS document, the timestamp syntax is deprecated. This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

    TIMESTAMP Transact-SQL timestamp data type is different from the timestamp data type defined in the ISO standard. It has nothing to do with date and time. Timestamp is a synonym for rowversion.

    Rowversion data type is not a date or time data type. Each database has a counter that is incremented for each insert or update operation that is performed on a table that contains a rowversion column within the database. This counter is the database rowversion. This tracks a relative time within a database, not an actual time that can be associated with a clock. A table can have only one rowversion column. Every time that a row with a rowversion column is modified or inserted, the incremented database rowversion value is inserted in the rowversion column.

    Please refer to MS document rowversion (Transact-SQL) to get more information.

    Best regards,
    Cathy


    If the response is helpful, please click "Accept Answer" and upvote it, thank you.

    0 comments No comments