简体   繁体   English

无法将数据类型nvarchar转换为数字

[英]Cannot convert datatype nvarchar to numeric

I import data from from an Excel file into a SQL Server database with the following query. 我使用以下查询将数据从Excel文件导入SQL Server数据库。 The Excel file has all values as string types ( ' before every cells). Excel文件的所有值都为字符串类型(每个单元格之前为' )。

I get this error when I import it."Cannot convert datatype nvarchar to numeric" 导入时收到此错误。“无法将数据类型nvarchar转换为数值”

If I remove the two columns SalePrice and Price2 from importing, then the import is successful. 如果我从导入中删除了两列SalePrice和Price2,则导入成功。

The datatypes of my table 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

The query I am using is: 我正在使用的查询是:

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

One thing to keep in mind is that you may have some invalid data in the given column that's throwing the error(s). 要记住的一件事是,给定列中可能包含一些无效数据,这些数据会引发错误。 For example if you have a Numeric column and have letters or inproperly formatted numeric values, this error will be thrown. 例如,如果您有一个Numeric列,并且包含字母或格式不正确的数值,则将引发此错误。 Check for values in your cells that aren't correct first. 首先检查单元格中不正确的值。 This includes trailing or leading spaces as well. 这也包括尾随或前导空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM