【问题标题】:Conversion of the nvarchar value '3001822585' overflowed an int columnnvarchar 值“3001822585”的转换溢出了一个 int 列
【发布时间】:2014-04-21 05:38:13
【问题描述】:

我使用以下方法将 Excel 文件导入 SQL Server。

Excel 文件的所有值都是字符串。我可以导入除BarcodeSalePricePrice2 之外的文件。我收到错误

nvarchar 值 '3001822585'(barcode) 的转换溢出了一个 int 列

代码:

         SqlCommand sqlcmd = new SqlCommand
         (@"MERGE Inventory AS target
         USING (SELECT
            LocalSKU, ItemName, QOH, Price, Discontinued,Barcode, Integer2
            ,Integer3, SalePrice, SaleOn, Price2 FROM @source)
            AS Source ON (Source.LocalSKU = target.LocalSKU)
           WHEN MATCHED THEN
           UPDATE 
           SET ItemName = source.ItemName,
           Price = source.Price,
           Discontinued = source.Discontinued,
           Barcode = source.Barcode,
           Integer2 = source.Integer2,
           Integer3 = source.QOH,
           SalePrice = source.SalePrice,
           SaleOn = source.SaleOn,
           Price2 = source.Price2;", sqlconn);

           SqlParameter param;
           param = sqlcmd.Parameters.AddWithValue("@source", dr);
           param.SqlDbType = SqlDbType.Structured;
           param.TypeName = "dbo.InventoryType";

           sqlconn.Open();
           sqlcmd.ExecuteNonQuery();
           sqlconn.Close();

数据库::

 LocalSKU varchar(200),
 ItemName varchar(200),
 QOH int,
 Price decimal(19,4),
 Discontinued bit,
 Barcode int,
 Integer2 int,
 Integer3 int,
 SalePrice decimal(19,4),
 SaleOn bit,
 Price2 decimal(19,4)

dbo.InventoryType 是:

   CREATE TYPE [dbo].[InventoryType] AS TABLE
   (
      [LocalSKU] [varchar](200) NOT NULL,
      [ItemName] [varchar](200) NULL,
      [QOH] [int] NULL,
      [Price] [decimal](19, 4) NULL,
      [Discontinued] [bit] NULL,
      [Barcode] [varchar](25) NULL,
      [Integer2] [int] NULL,
      [Integer3] [int] NULL,
      [SalePrice] [decimal](19, 4) NULL,
      [SaleOn] [bit] NULL,
      [Price2] [decimal](19, 4) NULL
   )
   GO

如何在表值参数中转换数据类型?示例将不胜感激。

【问题讨论】:

    标签: sql sql-update type-conversion table-valued-parameters


    【解决方案1】:

    使用 unsignedInt 数据类型。

    int range - 4 bytes, -2,147,483,648 to +2,147,483,647 
    unsignedInt range - 0 to 4,294,967,295 
    

    您的条形码值 3001822585 超过了最大 int 值 2,147,483,647 并因此引发错误

    编辑

    unigned int 在 SQL Server 中找不到,但是 MySQL 支持它。 对于 SQL Server,您必须使用 bigINT

    bigInt range - 8 bytes -2^63 to 2^63-1 
    

    所以将 Barcode 列的数据类型更改为 bigInt 。如果您认为其他列的值可以超过 int range ,那么也要更改它们的数据类型。

    【讨论】:

      【解决方案2】:

      SQL 语句:

      SELECT max( Mother_ID)+1 FROM Mother
      

      http://i.stack.imgur.com/ACKAW.jpg 消息 248,第 16 层,状态 1,第 1 行 nvarchar 值 '182512900911500120' 的转换溢出了一个 int 列。

      问题是由于数字182512900911500120超出了int范围引起的。如果我们转换成 bigint 就可以了。

      SELECT max(convert(bigint,Mother_ID))+1 as MID FROM Mother
      

      http://i.stack.imgur.com/ZLSNR.jpg 结果 182512900911500121

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多