use*_*244 5 sql type-conversion table-valued-parameters sql-update
我使用以下将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();
Run Code Online (Sandbox Code Playgroud)
数据库::
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)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
如何在表值参数中转换数据类型?例子将不胜感激.
使用unsignedInt数据类型.
int range - 4 bytes, -2,147,483,648 to +2,147,483,647
unsignedInt range - 0 to 4,294,967,295
Run Code Online (Sandbox Code Playgroud)
条形码值3001822585超过最大值2,147,483,647,因此抛出错误
编辑
unigned int在SQL Server中找不到,但MySQL支持它.对于SQL Server,您必须使用bigINT
bigInt range - 8 bytes -2^63 to 2^63-1
Run Code Online (Sandbox Code Playgroud)
所以将Barcode列的数据类型更改为bigInt .如果您认为其他列值可能超出int范围,那么也要更改其数据类型.
| 归档时间: |
|
| 查看次数: |
37133 次 |
| 最近记录: |