CREATE TABLE #t
(
  [myid] [int] IDENTITY(2147483645,1) NOT NULL,
  [mydt] [datetime] NOT NULL  DEFAULT (getdate()),
  [myno] [int] NULL
)insert into #t (myno) values(1)
insert into #t (myno) values(2)
insert into #t (myno) values(3)select * from #tmyid            mydt                    myno
--------------------------------------------
2147483645 2008-10-26 10:09:58.077  1
2147483646 2008-10-26 10:09:58.077  2
2147483647 2008-10-26 10:09:58.077  3
insert into #t (myno) values(4)消息 8115,级别 16,状态 1,第 1 行
将 IDENTITY 转换为数据类型 int 时出现算术溢出错误。
发生算术溢出。

解决方案 »

  1.   

    可以把标识列的数据类型改为 bigint :
    CREATE TABLE #t
    (
      [myid] [bigint] IDENTITY(1,1) NOT NULL,
      [mydt] [datetime] NOT NULL  DEFAULT (getdate()),
      [myno] [int] NULL
    )
      

  2.   

    和你定义的字段类型上限一样bigint  从 –2^63 (–9,223,372,036,854,775,808) 到 2^63–1 (9,223,372,036,854,775,807)
    整数数据,从 –2^63 (–9,223,372,036,854,775,808) 到 2^63–1 (9,223,372,036,854,775,807)。存储大小为 8 字节。integer –2^31 (-2,147,483,648) 到 2^31–1 (2,147,483,647)
    整数数据,从 –2^31 (-2,147,483,648) 到 2^31–1 (2,147,483,647)。存储大小为 4 字节。smallint  –32,768 到 32,767
    整数数据,从 –32,768 到 32,767。存储大小为 2 字节。
     
    tinyint  从 0 到 255
    整数数据,从 0 到 255。存储大小为 1 字节。
     
      

  3.   

    bigint 是64位有符号整数,表示值介于 -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807 之间的整数。
      

  4.   

    最大值就是所设类型的最大值,如将类型设为tinyint,最大值就是127
      

  5.   


    tinyint 的值范围是 从 0 到 255 
    数据类型  范围  存储  
    bigint   -2^63 (-9,223,372,036,854,775,808) 到 2^63-1 (9,223,372,036,854,775,807)  8 字节
     
    int      -2^31 (-2,147,483,648) 到 2^31-1 (2,147,483,647)  4 字节
     
    smallint  -2^15 (-32,768) 到 2^15-1 (32,767)   2 字节
     
    tinyint   0 到 255   1 字节
     
      

  6.   

    甚至可以把自增列的数据类型定为 decimal(38,0) :CREATE TABLE #t(
      [myid] decimal(38,0) IDENTITY(99999999999999999999999999999999999997,1) NOT NULL,
      [mydt] datetime NOT NULL  DEFAULT (getdate()),
      [myno] int NULL
    )insert into #t (myno) values(1)
    insert into #t (myno) values(2)
    insert into #t (myno) values(3)select * from #tmyid                                    mydt                    myno
    --------------------------------------------------------------------
    99999999999999999999999999999999999997 2008-10-26 10:29:58.343   1
    99999999999999999999999999999999999998 2008-10-26 10:29:58.357   2
    99999999999999999999999999999999999999 2008-10-26 10:29:58.357   3
    insert into #t (myno) values(4)消息 8115,级别 16,状态 1,第 1 行
    将 IDENTITY 转换为数据类型 decimal 时出现算术溢出错误。
    发生算术溢出。
      

  7.   

    “decimal(38,0)”
    38位都能溢出啊?MSSQL2005有没有自动回到起始值的功能?
    如果没有这个功能的话,那岂不是在每次插入的时候都要判断自增长字段是否到了上限?