将varchar值'products 2'转换为数据类型int时转换失败。 [英] Conversion failed when converting the varchar value 'products 2' to data type int.

查看:81
本文介绍了将varchar值'products 2'转换为数据类型int时转换失败。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要以下问题的帮助。我写了剧本,但它给了我一个错误。有三个表



用户:(UserId,EmailAddress,FirstName,LastName)

产品:(ProductId,ProductName)

下载:(DownloadID,UserID,DownloadDate,FileName,ProductID)





在下载表中添加三行:一行用户1和产品2的行;一个用户2和产品1;一个用户2和产品2.使用GETDATE函数将当前日期和时间插入DownloadDate列。







谢谢



我尝试过:



我写的脚本如下:



插入下载(UserId,ProductID,DownloadDate)

VALUES

('1',产品2),GETDATE()),

('2',产品1,GETDATE()),

('2 ',产品2,GETDATE());



但我收到以下错误:

sg 245,Level 16,State 1 ,第1行

将varchar值'Products 2'转换为数据类型int时转换失败。



如何更正此问题?



谢谢

解决方案

简单:产品2不是整数! (另外,那里有一个虚假的关闭括号)即使作为一个字符串,它也需要是一个引用值:

  INSERT   INTO 下载(UserId,ProductID,DownloadDate)
VALUES
' 1'' Products 2',GETDATE()),
' 2'' Products 1',GETDATE()),
' 2'' Products 2',GETDATE());

但是......产品和下载表中的ProductID是什么数据类型 - 他们需要是相同的,错误信息的推断是至少其中一个是传递VARCHAR(或NVARCHAR)值时的INT。



我仔细查看您的表定义并检查您尝试使用的类型:引用的UserID有点意味着它也是一个字符串,它可能应该是一个整数,而不是在INSERT语句中引用。


   -   字符串值必须在单引号('Products2')下,但不能在整数下。 
- 如果您的DownloadDate列数据类型是varchar,则使用convert(Varchar(20),GETDATE(),101)
插入 进入下载(UserId,ProductID,DownloadDate)

VALUES
1 ' Products2',GETDATE()),
2 ' Products1',GETDATE()),
2 ' Products2',GETDATE());


Hello, I need help with the question below. I wrote the script but it is giving me an error. There are three tables

Users:(UserId,EmailAddress, FirstName, LastName)
Products:(ProductId, ProductName)
Downloads:(DownloadID,UserID,DownloadDate,FileName,ProductID)


Add three rows to the Downloads table: one row for user 1 and product 2; one for user 2 and product 1; and one for user 2 and product 2. Use the GETDATE function to insert the current date and time into the DownloadDate column.



Thank you

What I have tried:

The script I wrote is as follows:

Insert into Downloads (UserId, ProductID, DownloadDate)
VALUES
('1',Products 2),GETDATE()),
('2',Products 1,GETDATE()),
('2',Products 2,GETDATE());

But I am getting the following error:
sg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'Products 2' to data type int.

How can I correct this?

Thank you

解决方案

Simple: "Products 2" is not a integer! (Plus, there is a spurious close bracket in there) Even as a string, it would need to be a quoted value:

INSERT INTO Downloads (UserId, ProductID, DownloadDate) 
VALUES
   ('1', 'Products 2',GETDATE()),
   ('2', 'Products 1',GETDATE()),
   ('2', 'Products 2',GETDATE());

But ... what datatype is ProductID, in both the Products and Downloads tables - they need to be the same, and the inference from the error message is that at least one of them is an INT while you are passing a VARCHAR (or NVARCHAR) value.

I'd have a close look at your table definitions and check what types you are trying to use: the quoted UserID kinda implies that's a string as well and it probably should be an integer, and not quoted in the INSERT statement.


--String values must be under single quotations('Products2') but not integers.
--IF your DownloadDate column datatype is varchar then use convert(Varchar(20),GETDATE(),101) 
Insert into Downloads (UserId, ProductID, DownloadDate) 

                                  VALUES
                                      (1,'Products2',GETDATE()),
                                      (2,'Products1',GETDATE()),
                                      (2,'Products2',GETDATE());


这篇关于将varchar值'products 2'转换为数据类型int时转换失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆