对于存储过程,我收到以下异常/错误。
AL-DMK2_1
“将 varchar 值转换为数据类型 int时转换失败。”
存储过程:
ALTER PROCEDURE dbo.sp_generateNewRequestId
(
@Customer varchar(50),
@Network_Type varchar(25),
@Market_Name varchar(50),
@NewRequestId varchar(50) OUTPUT
)
AS
Begin
Declare @MarketCode as varchar(10);
Declare @CustomerPrefix as varchar(10);
SET @MarketCode = (select Market_Code from Customer_Market_Details where Customer = @Customer and Network_Type = @Network_Type and Market_Name = @Market_Name);
SET @CustomerPrefix = (select Customer_Prefix from Customer_Market_Details where Customer = @Customer and Network_Type = @Network_Type and Market_Name = @Market_Name);
Declare @RequestIDPrefix as varchar(20);
SET @RequestIdPrefix = @CustomerPrefix + @MarketCode + '_';
-- Count how many requests you already have with the same request ID Prefix.
Declare @RequestCount as int;
SET @RequestCount = (Select count (*) from request_details where request_id like @RequestIdPrefix + '%');
SET @NewRequestId = @RequestIdPrefix + CAST ((@RequestCount + 1) AS varchar);
While (Exists (Select * from request_details where request_id = @NewRequestId ) )
Begin
SET @RequestCount = @RequestCount + 1;
SET @NewRequestId = @RequestIdPrefix + CAST ((@RequestCount + 1) AS varchar);
end
RETURN @NewRequestId;
END
调用存储过程:
using (SqlConnection connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["dbString"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("dbo.sp_generateNewRequestId", connection))
{
command.Parameters.AddWithValue("@Customer", customer);
command.Parameters.AddWithValue("@Network_Type", technology);
command.Parameters.AddWithValue("@Market_Name", market);
command.Parameters.Add("@NewRequestId", SqlDbType.VarChar).Direction = ParameterDirection.Output;
command.Parameters["@NewRequestId"].Size = 50;
command.CommandType = CommandType.StoredProcedure;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
观察:
当输出参数设置为@RequestCount 时,过程成功返回@RequestCount。
varchar
但是,如果尝试输出任何其他值,则会抛出具有上述错误的异常。执行失败在行command.ExecuteNonQuery()
。
在线提供的解决方案均不适用于此错误。感谢你的帮助!
谢谢!