[英]Error when doing a SQL Server query
I am doing this query in a stored procedure: SELECT TOP 1 [Employe] FROM Transactions WHERE [Employe]=@Name AND Date=@Date
我在存储过程中执行此查询:
SELECT TOP 1 [Employe] FROM Transactions WHERE [Employe]=@Name AND Date=@Date
It is supposed to return an employee ID (int). 它应该返回一个员工ID(int)。 In my application (ASP.NET C#) I get this error when, with a DataReader, I read a line: *Conversion failed when converting the varchar value * to data type int.
在我的应用程序(ASP.NET C#)中,当使用DataReader读取一行时,我收到此错误:*将varchar值*转换为数据类型int时转换失败。 *
*
What exactly is the type * ? 究竟是什么类型*? And I am not trying to convert anything.
我并不是想改变任何东西。
Looks like you're sleecting the Employe
column which I'm guessing is varchar
. 看起来你正在调整
Employe
列,我猜测它是varchar
。 I'm assuming you want to select the ID
column. 我假设您要选择
ID
列。
SELECT TOP 1 [ID] FROM Transactions WHERE [Employe]=@Name AND Date=@Date
I think probably your @Name
parameter is specified as an VARCHAR
, but the Employe field in the table is an INT
field. 我认为您的
@Name
参数可能指定为VARCHAR
,但表中的Employe字段是INT
字段。
Try this: 尝试这个:
SELECT TOP 1 [Employe] FROM Transactions WHERE CAST([Employe] AS VARCHAR)=@Name AND Date=@Date
Most likely you are passing @Name
parameter as a varchar
where Employe
field is an int
field. 最有可能您将
@Name
参数作为varchar
传递,其中Employe
字段是int
字段。
Or you could be trying to assign your resulting value of Employe
to an int
datatype then you would be seeing this error. 或者,您可能试图将
Employe
的结果值分配给一个int
数据类型,那么您将看到此错误。
Please post your Transactions
table definition and your stored procedure code to further troubleshoot. 请发布您的
Transactions
表定义和您的存储过程代码,以进一步解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.