【问题标题】:SQL Server 2008: what happened if identity oversteps a maximal value of int?SQL Server 2008:如果标识超过 int 的最大值会发生什么?
【发布时间】:2011-01-24 10:43:42
【问题描述】:

假设我们有一张桌子:

create table MYTABLE (
 id int IDENTITY(1,1)
,name varchar(10)
)

我们必须在表中插入很多行。

有人知道当生成的标识值超过最大整数值 (2^63-1) 时会发生什么吗?

【问题讨论】:

    标签: sql-server sql-server-2008 identity identity-column


    【解决方案1】:

    一个例子

    create table dbo.MYTABLE (
     id tinyint IDENTITY(254,1)
    ,name varchar(10)
    )
    GO
    INSERT dbo.MYTABLE (name) VALUES ('row 254')
    GO
    INSERT dbo.MYTABLE (name) VALUES ('row 255')
    GO
    INSERT dbo.MYTABLE (name) VALUES ('broke')
    GO
    

    给予

    Msg 8115, Level 16, State 1, Line 1
    Arithmetic overflow error converting IDENTITY to data type tinyint.
    Arithmetic overflow occurred.
    

    【讨论】:

      【解决方案2】:

      会发生错误,插入会丢失。

      消息 8115,第 16 级,状态 1,第 2 行 将 IDENTITY 转换为数据类型 int 的算术溢出错误。 发生算术溢出。

      【讨论】:

      • 能否提供一个文档链接。
      【解决方案3】:

      您可以使用非常小的标识列轻松测试这一点,例如 decimal(1,0)

      create table IdentityOverflow (id decimal(1,0) identity)
      while 1=1
          insert IdentityOverflow default values
      

      就像 Oded 所说,这打印出来:

      Arithmetic overflow error converting IDENTITY to data type decimal.
      

      这甚至适用于最大的整数:

      create table IdentityOverflow (
          id decimal(38,0) identity(1,10000000000000000000000000000000000000))
      while 1=1
          insert IdentityOverflow default values
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-22
        • 2010-12-08
        • 2020-04-27
        • 1970-01-01
        • 1970-01-01
        • 2011-07-05
        相关资源
        最近更新 更多