Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of the nvarchar value '3001822585' overflowed an int column

I use following to import Excel file to SQL Server.

The Excel file has all the values as string. I am able to import the file except Barcode, SalePrice and Price2. I get error

Conversion of the nvarchar value '3001822585'(barcode) overflowed an int column

Code:

         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();

Database::

 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 is:

   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

How to cast the datatype in table valued parameter? Example will be appreciated.

like image 324
user2525244 Avatar asked Apr 21 '14 05:04

user2525244


2 Answers

Use unsignedInt datatype.

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

Your barcode value 3001822585 is exceeding max int value 2,147,483,647 and so throws error

Edit

unigned int are not found in SQL Server , however MySQL supports it. For SQL Server you will have to use bigINT

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

So change data type of Barcode column to bigInt . If you think other column values can exceed int range , then change their datatype also.

like image 149
Mudassir Hasan Avatar answered Nov 12 '22 04:11

Mudassir Hasan


SQl Statement:

SELECT max( Mother_ID)+1 FROM Mother

http://i.stack.imgur.com/ACKAW.jpg Msg 248, Level 16, State 1, Line 1 The conversion of the nvarchar value '182512900911500120' overflowed an int column.

The problem is caused due to the number 182512900911500120 exceeds int range. If we convert in to bigint than it works.

i.e.

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

http://i.stack.imgur.com/ZLSNR.jpg Result 182512900911500121

like image 44
Samiran Nath Avatar answered Nov 12 '22 03:11

Samiran Nath