我使用以下命令将Excel文件导入SQL Server。
Excel文件的所有值都是字符串。我可以导入除Barcode
,SalePrice
和Price2
之外的文件。我收到错误
nvarchar值'3001822585'(条形码)的转换溢出了一个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
如何在表值参数中转换数据类型?示例将不胜感激。
答案 0 :(得分:7)
使用unsignedInt数据类型。
int range - 4 bytes, -2,147,483,648 to +2,147,483,647
unsignedInt range - 0 to 4,294,967,295
您的条形码值 3001822585 超出最大值 2,147,483,647 ,因此抛出错误
修改强>
在SQL Server中找不到 unigned int
,但MySQL支持它。
对于SQL Server,您必须使用 bigINT
bigInt range - 8 bytes -2^63 to 2^63-1
将Barcode
列的数据类型更改为bigInt。如果您认为其他列值可能超出int范围,那么也要更改其数据类型。
答案 1 :(得分:1)
SQl声明:
SELECT max( Mother_ID)+1 FROM Mother
http://i.stack.imgur.com/ACKAW.jpg Msg 248,Level 16,State 1,Line 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