Quelque chose que je ne connaissais pas était les fonctions d'identité (@@identity, SCOPE_IDENTITY et IDENT_CURRENT) renvoient une valeur décimale (38,0), peu importe ce que votre champ d'identité locale est définie comme.
Comme d'autres l'ont indiqué, le message d'erreur sera la même nature Arithmetic overflow error converting IDENTITY to data type X
Et pendant que vous avez posé la question à propos de SQL Server, mon histoire d'horreur de MySQL 4.trash est une application héritée à un ancien poste avait une identité définie sur tinyint. Quand cela débordait, il n'a pas la bombe, juste gardé insérer des lignes avec le même identifiant (je sais, le PK aurait dû l'empêcher, mais il était un vraiment mal conception db)
Stefanelli déjà fourni @ Joe un cadre pour générer des erreurs, mais pour ma propre éducation, je l'ai soufflé pour couvrir les bigints et les décimales.
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
Version 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
version décimale
----------------------------------------------
-- 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
Et si vous utilisez SET ARITHABORT OFF avant l'INSERT? :-) Mmmh ... La même chose. – xanatos