smallint 的算术溢出错误——但数据类型是 int?

Posted

技术标签:

【中文标题】smallint 的算术溢出错误——但数据类型是 int?【英文标题】:Arithmetic overflow error for smallint--but the datatype is int? 【发布时间】:2015-12-11 22:52:52 【问题描述】:

我有一个将值插入到表中的存储过程:

ALTER PROCEDURE [dbo].[spSaveAddresses_LegalDescriptions_Determinations]
   @idAddresses_LegalDescriptions_Determinations INT    = NULL OUTPUT,
   @idLogin INT = -32768,
   @idAddress INT = NULL,
   @idLegalDescription INT = NULL,
   @idDetermination INT = NULL
AS
   SET NOCOUNT ON
   SET ROWCOUNT 0
   SET XACT_ABORT ON

   BEGIN TRY
      /* Declare and initialize variables */
      DECLARE   @RC         INT,
                @TranCount  INT

      SELECT    
          @RC = 0,
          @TranCount = @@TRANCOUNT

      /* Insert record into Table #1] */
      IF @TranCount = 0 
      BEGIN TRANSACTION
      BEGIN
           INSERT INTO [TOD].[dbo].[Table#1](idAddress, idLegalDescription,
                                             idDetermination, UpdatedidLogin,
                                             UpdatedDate)
           VALUES(@idAddress, @idLegalDescription,
                  @idDetermination, @idLegalDescription, GETDATE())
     END

     IF @TranCount = 0 AND (XACT_STATE()) = 1 
        COMMIT TRANSACTION
END TRY

BEGIN CATCH
IF (XACT_STATE()) = -1 ROLLBACK TRANSACTION
    EXEC [TOD].[dbo].[spRethrowError]
    SET @RC = 1
END CATCH

SET NOCOUNT OFF
SET ROWCOUNT 0
RETURN @RC
GO

表#1的结构:

CREATE TABLE [dbo].[Addresses_LegalDescriptions_Determinations]
(
    [idAddresses_LegalDescriptions_Determinations] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [idAddress] [int] NULL,
    [idLegalDescription] [int] NULL,
    [idDetermination] [int] NULL,
    [UpdatedidLogin] [smallint] NOT NULL 
         CONSTRAINT [DF_Addresses_LegalDescriptions_Determinations_UpdatedidLogin]  DEFAULT ((0)),
    [UpdatedDate] [datetime] NOT NULL,

    CONSTRAINT [pkAddresses_LegalDescriptions_Determinations] 
        PRIMARY KEY CLUSTERED ([idAddresses_LegalDescriptions_Determinations] ASC)
) ON [DATA]
GO

ALTER TABLE [dbo].[Addresses_LegalDescriptions_Determinations] WITH NOCHECK 
   ADD CONSTRAINT [FK_Addresses_LegalDescriptions_Determinations_Addresses] 
       FOREIGN KEY([idAddress])
       REFERENCES [dbo].[Addresses] ([idAddress])
            ON DELETE CASCADE
            NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[Addresses_LegalDescriptions_Determinations] 
      CHECK CONSTRAINT [FK_Addresses_LegalDescriptions_Determinations_Addresses]
GO

ALTER TABLE [dbo].[Addresses_LegalDescriptions_Determinations] WITH NOCHECK   
ADD CONSTRAINT [FK_Addresses_LegalDescriptions_Determinations_Determinations]   
    FOREIGN KEY([idDetermination])
    REFERENCES [dbo].[Determinations] ([idDetermination])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[Addresses_LegalDescriptions_Determinations] CHECK    CONSTRAINT [FK_Addresses_LegalDescriptions_Determinations_Determinations]
GO

ALTER TABLE [dbo].[Addresses_LegalDescriptions_Determinations]  WITH NOCHECK   ADD  CONSTRAINT     [FK_Addresses_LegalDescriptions_Determinations_LegalDescriptions] FOREIGN   KEY([idLegalDescription])
REFERENCES [dbo].[LegalDescriptions] ([idLegalDescription])
ON DELETE CASCADE
NOT FOR REPLICATION 
GO

ALTER TABLE [dbo].[Addresses_LegalDescriptions_Determinations] CHECK    CONSTRAINT [FK_Addresses_LegalDescriptions_Determinations_LegalDescriptions]
GO

导致错误的表结构:

CREATE TABLE [dbo].[LegalDescriptions](
[idLegalDescription] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[LotNumber] [varchar](15) NULL,
[BlockNumber] [varchar](15) NULL,
[SubDivision] [varchar](255) NULL,
[UpdatedDate] [datetime] NOT NULL,
[InsertedDate] [datetime] NULL,
[SubDivisionPhase] [varchar](5) NULL,
[Township] [varchar](20) NULL,
[UpdatedidLogin] [smallint] NOT NULL CONSTRAINT [DF_LegalDescriptions_UpdatedidLogin]  DEFAULT ((0)),
[InsertedIDLogin] [smallint] NULL,
[LegalDescription] [varchar](255) NULL,
[PinNumber] [varchar](30) NULL,
[Range] [varchar](15) NULL,
[SectionLegalDescription] [varchar](15) NULL,
 CONSTRAINT [pkLegalDescriptions] PRIMARY KEY CLUSTERED 
(
    [idLegalDescription] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,  ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [DATA]
) ON [DATA]

GO

SET ANSI_PADDING OFF
GO

我尝试执行存储过程时收到的错误:

消息 50000,级别 16,状态 1,过程 spRethrowError,第 42 行 错误 220,级别 16,状态 1,过程 spSaveAddresses_LegalDescriptions_Determinations,第 42 行 消息:数据类型 smallint 的算术溢出错误,值 = 171922。

我的问题:

此列的所有引发错误 [idLegalDescription] 的数据类型都是 INT,为什么 stored procedure 调用 smallint 数据类型?我敢肯定,我错过了一些简单的东西。

【问题讨论】:

【参考方案1】:

你的专栏

 [UpdatedidLogin] [smallint] 

填充
@idLegalDescription                             INT

【讨论】:

哇!多么可怕的错字。我知道这很简单。废话,谢谢!时间一到,我会接受这个作为答案。

以上是关于smallint 的算术溢出错误——但数据类型是 int?的主要内容,如果未能解决你的问题,请参考以下文章

货币类型的算术溢出错误

CAST/CONVERT 问题 算术溢出错误

CAST / CONVERT问题算术溢出错误

我的SQL 运行提示 DBA数据层_错误将 expression 转换为数据类型 int 时发生算术溢出错误

将 varchar 转换为数字数据类型时出现算术溢出错误。找不到存储过程''。?

速达 将numeric转换为数据类型numeric时发生算术溢出错误