可能重複:
What happens to the primary key Id? when it goes over the limit?如果達到標識列的最大值,會附加什麼?
,如果有一個SQL Server表與標識列究竟會追加(說一個int)到達INT的最大容量是多少?
回到開頭?
假設線增長100 100。每次我插入100條新線,我刪除100分舊的。
謝謝您的回答 問候
可能重複:
What happens to the primary key Id? when it goes over the limit?如果達到標識列的最大值,會附加什麼?
,如果有一個SQL Server表與標識列究竟會追加(說一個int)到達INT的最大容量是多少?
回到開頭?
假設線增長100 100。每次我插入100條新線,我刪除100分舊的。
謝謝您的回答 問候
,當你超過最大int值你會得到一個算術溢出錯誤。
試試:
DECLARE @t TABLE (
id INT IDENTITY (2147483647,1),
name VARCHAR(100)
)
INSERT INTO @t (name) VALUES ('Joe')
INSERT INTO @t (name) VALUES ('Tim')
它不會讓你插入更多行。
的東西我不知道的是身份的功能(@@identity,SCOPE_IDENTITY和IDENT_CURRENT)返回一個十進制(38,0)值,無論什麼你的本地身份字段定義如。
正如其他人所指出的,該錯誤信息是一樣的性質 Arithmetic overflow error converting IDENTITY to data type X
而當你問到SQL Server中的問題,我的MySQL 4.trash的恐怖故事是一個老工作的遺留應用程序有在tinyint上定義的標識。當溢出,它不彈了出來,只是不停地將具有相同id行(我才知道,PK應該阻止它,但它是一個真正不良設計DB)
@Joe Stefanelli已經提供了爲了我自己的教育而產生錯誤的框架,我將它吹掉以涵蓋bigint和decimal。
SET NOCOUNT ON
IF EXISTS (select 1 from sys.tables T WHERE T.name = 'Tim' AND SCHEMA_NAME(t.schema_id) = 'dbo')
BEGIN
DROP TABLE dbo.Tim
END
IF EXISTS (select 1 from sys.tables T WHERE T.name = 'Tim_decimal' AND SCHEMA_NAME(t.schema_id) = 'dbo')
BEGIN
DROP TABLE dbo.Tim_decimal
END
IF EXISTS (select 1 from sys.tables T WHERE T.name = 'Tim_bigint' AND SCHEMA_NAME(t.schema_id) = 'dbo')
BEGIN
DROP TABLE dbo.Tim_bigint
END
-- http://msdn.microsoft.com/en-us/library/ms187342.aspx
CREATE TABLE
dbo.Tim
(
tim_id int identity(2147483646 , 1) NOT NULL PRIMARY KEY
, val int
)
BEGIN TRY
-- consumes the first value
INSERT INTO
dbo.Tim
SELECT
0 AS number
SELECT SCOPE_IDENTITY() AS last_int_identity
-- this insert brings us to the edge
INSERT INTO
dbo.Tim
SELECT
1 AS number
SELECT SCOPE_IDENTITY() AS last_int_identity
-- This one goes kaboom
--Msg 8115, Level 16, State 1, Line 27
--Arithmetic overflow error converting IDENTITY to data type int.
INSERT INTO
dbo.Tim
SELECT
-1 AS number
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
, ERROR_SEVERITY() AS ErrorSeverity
, ERROR_STATE() AS ErrorState
, ERROR_PROCEDURE() AS ErrorProcedure
, ERROR_LINE() AS ErrorLine
, ERROR_MESSAGE() AS ErrorMessage
END CATCH
BIGINT版本
----------------------------------------------
-- Try again with big ints
----------------------------------------------
SET NOCOUNT ON
CREATE TABLE
dbo.Tim_bigint
(
tim_id bigint identity(9223372036854775806, 1) NOT NULL PRIMARY KEY
, val int
)
BEGIN TRY
-- consumes the first value
INSERT INTO
dbo.Tim_bigint
SELECT
0 AS number
SELECT SCOPE_IDENTITY() AS last_bigint_identity
-- this insert brings us to the edge
INSERT INTO
dbo.Tim_bigint
SELECT
1 AS number
SELECT SCOPE_IDENTITY() AS last_bigint_identity
-- This one goes kaboom
--Msg 8115, Level 16, State 1, Line 27
--Arithmetic overflow error converting IDENTITY to data type bigint.
INSERT INTO
dbo.Tim_bigint
SELECT
-1 AS number
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
, ERROR_SEVERITY() AS ErrorSeverity
, ERROR_STATE() AS ErrorState
, ERROR_PROCEDURE() AS ErrorProcedure
, ERROR_LINE() AS ErrorLine
, ERROR_MESSAGE() AS ErrorMessage
END CATCH
十進制版本
----------------------------------------------
-- Let's really max this out
----------------------------------------------
SET NOCOUNT ON
CREATE TABLE
dbo.Tim_decimal
(
-- 10^38 -1
-- 10^37 = 10000000000000000000000000000000000000
-- 10^38 = 100000000000000000000000000000000000000
tim_id decimal(38,0) identity(99999999999999999999999999999999999998, 1) NOT NULL PRIMARY KEY
, val int
)
BEGIN TRY
-- consumes the first value
INSERT INTO
dbo.Tim_decimal
SELECT
0 AS number
SELECT SCOPE_IDENTITY() AS last_decimal_identity
-- this insert brings us to the edge
INSERT INTO
dbo.Tim_decimal
SELECT
1 AS number
SELECT SCOPE_IDENTITY() AS last_decimal_identity
-- This one goes kaboom
--Msg 8115, Level 16, State 1, Line 27
--Arithmetic overflow error converting IDENTITY to data type decimal.
INSERT INTO
dbo.Tim_decimal
SELECT
-1 AS number
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
, ERROR_SEVERITY() AS ErrorSeverity
, ERROR_STATE() AS ErrorState
, ERROR_PROCEDURE() AS ErrorProcedure
, ERROR_LINE() AS ErrorLine
, ERROR_MESSAGE() AS ErrorMessage
END CATCH
如果你在INSERT之前使用SET ARITHABORT OFF? :-)嗯......一樣。 – xanatos