SQL Server: Cannot insert an explicit value into a timestamp column

后端 未结 7 2014
无人及你
无人及你 2020-11-30 09:25

When using this statement

create table demo (
    ts timestamp
)

insert into demo select current_timestamp

I get the following error:

相关标签:
7条回答
  • 2020-11-30 09:57

    There is some good information in these answers. Suppose you are dealing with databases which you can't alter, and that you are copying data from one version of the table to another, or from the same table in one database to another. Suppose also that there are lots of columns, and you either need data from all the columns, or the columns which you don't need don't have default values. You need to write a query with all the column names.

    Here is a query which returns all the non-timestamp column names for a table, which you can cut and paste into your insert query. FYI: 189 is the type ID for timestamp.

    declare @TableName nvarchar(50) = 'Product';
    
    select stuff(
        (select 
            ', ' + columns.name
        from 
            (select id from sysobjects where xtype = 'U' and name = @TableName) tables
            inner join syscolumns columns on tables.id = columns.id
        where columns.xtype <> 189
        for xml path('')), 1, 2, '')
    

    Just change the name of the table at the top from 'Product' to your table name. The query will return a list of column names:

    ProductID, Name, ProductNumber, MakeFlag, FinishedGoodsFlag, Color, SafetyStockLevel, ReorderPoint, StandardCost, ListPrice, Size, SizeUnitMeasureCode, WeightUnitMeasureCode, Weight, DaysToManufacture, ProductLine, Class, Style, ProductSubcategoryID, ProductModelID, SellStartDate, SellEndDate, DiscontinuedDate, rowguid, ModifiedDate
    

    If you are copying data from one database (DB1) to another database(DB2) you could use this query.

    insert DB2.dbo.Product (ProductID, Name, ProductNumber, MakeFlag, FinishedGoodsFlag, Color, SafetyStockLevel, ReorderPoint, StandardCost, ListPrice, Size, SizeUnitMeasureCode, WeightUnitMeasureCode, Weight, DaysToManufacture, ProductLine, Class, Style, ProductSubcategoryID, ProductModelID, SellStartDate, SellEndDate, DiscontinuedDate, rowguid, ModifiedDate)
    select ProductID, Name, ProductNumber, MakeFlag, FinishedGoodsFlag, Color, SafetyStockLevel, ReorderPoint, StandardCost, ListPrice, Size, SizeUnitMeasureCode, WeightUnitMeasureCode, Weight, DaysToManufacture, ProductLine, Class, Style, ProductSubcategoryID, ProductModelID, SellStartDate, SellEndDate, DiscontinuedDate, rowguid, ModifiedDate 
    from DB1.dbo.Product
    
    0 讨论(0)
  • 2020-11-30 10:02

    If you have a need to copy the exact same timestamp data, change the data type in the destination table from timestamp to binary(8) -- i used varbinary(8) and it worked fine.

    This obviously breaks any timestamp functionality in the destination table, so make sure you're ok with that first.

    0 讨论(0)
  • 2020-11-30 10:05

    Assume Table1 and Table2 have three columns A, B and TimeStamp. I want to insert from Table1 into Table2.

    This fails with the timestamp error:

    Insert Into Table2
    Select Table1.A, Table1.B, Table1.TimeStamp From Table1
    

    This works:

    Insert Into Table2
    Select Table1.A, Table1.B, null From Table1
    
    0 讨论(0)
  • 2020-11-30 10:07

    How to insert current time into a timestamp with SQL Server:

    Answer: You can't, and here's why.

    In newer versions of SQL Server, timestamp is renamed to RowVersion. Rightly so, because timestamp is very misleading.

    SQL Server timestamp IS NOT set by the user and does not represent a date or time. timestamp is just a binary representation of a consecutive number, it's only good for making sure a row hasn't changed since it's been read.

    If you want to store a date or a time, do not use timestamp, you must use one of the other datatypes, like for example datetime, smalldatetime, date, time or DATETIME2

    For example:

    create table wtf (
        id INT,
        leet timestamp
    )
    
    insert into wtf (id) values (15)
    
    select * from wtf
    
    15    0x00000000000007D3 
    

    So timestamp is some kind of binary number. What if we try casting it to datetime?

    select CAST(leet AS datetime) from wtf
    
    1900-01-01 00:00:06.677
    

    The current year for me is not 1900. So I'm not sure what SQL Server is thinking.

    0 讨论(0)
  • 2020-11-30 10:13

    According to MSDN, timestamp

    Is a data type that exposes automatically generated, unique binary numbers within a database. timestamp is generally used as a mechanism for version-stamping table rows. The storage size is 8 bytes. The timestamp data type is just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime data type.

    You're probably looking for the datetime data type instead.

    0 讨论(0)
  • 2020-11-30 10:15

    create table demo ( id int, ts timestamp )

    insert into demo(id,ts) values (1, DEFAULT)

    0 讨论(0)
提交回复
热议问题