I'm trying to execute and consume a return value from a stored procedure that has a return parameter for a status code.
Here is the stored procedure:
DECLARE @RetVal int;
DECLARE @CustPK int;
BEGIN
SET @RetVal = 0;
SET NOCOUNT ON;
SET @CustPK = (SELECT CustPK FROM tCustomer WHERE WebCustomerId = @WebCustomerId);
-- Insert statements for procedure here
INSERT INTO tOrder(CustPK, WebOrderId, TxCode,DelCompany, DelContact, DelAddress1, DelAddress2, DelCity, DelPostcode, DelPhone, WhiteLabel, ServiceLevelPK, DeliveryCharge, ProductionSpeed, Paid, DateOrdered, [IP Contact], vc, ProductionTIME, ProductionCharge, VDiscountExVAT, VDiscountVAT,QuoteName,[Cost Centre Reference], [Client Reference], MethodOfTransport)
VALUES(@CustPK, @WebOrderId, @TxCode, @DelCompany, @DelContact, @DelAddress1, @DelAddress2, @DelCity, @DelPostcode, @DelPhone, @WhiteLabel, @DeliveryType, @DeliveryCharge, @ProductionSpeed, 1, @DateOrdered, 'NEW', @Voucher, @ProductionSpeed, @ProductionCharge, @VoucherDiscount, @VoucherVAT, @PersonalReference, @CostCentreReference, @ClientReference, @MethodOfTransport)
IF @@ERROR = 0
BEGIN
SET @RetVal = 1
END
RETURN @RetVal
This stored procedure is legacy and cannot be changed. I am trying to use Retval
in my application. I am using Entity Framework's Database.SqlQuery()
to execute this stored procedure.
I am creating a list of SqlParameter
and then call toArray()
so they can be read by EF. Normal stuff really.
Here is my EF code:
public string PlaceOrder(IPOPPlaceOrderRequest order)
{
try
{
string storedProcedure = BuildSQLExecutionString<IPOPPlaceOrderRequest>(order, "Web_NewOrder");
//storedProcedure = storedProcedure + ", @RetVal";
List<SqlParameter> parameters = BuildSQLParameters<IPOPPlaceOrderRequest>(order);
var retVal = new SqlParameter("RetVal", System.Data.SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output };
parameters.Add(retVal);
var test = tOrderRepository.RunSqlStoredProcedureSingle<IPOPPlaceOrderRequest>(storedProcedure, parameters.ToArray());
return "";
}
catch (Exception ex)
{
throw ex;
}
}
And here is RunSqlStoredProcedureSingle()
return unitOfWork.DataContext.Database.SqlQuery<T>(sql, parameters.ToArray()).FirstOrDefault();
This runs, but the value of retVal is never set to anything. I'm at a loss with this.
I have tried to do a @RetVal
in the stored procedure string with and without OUT at the end.
I have tried with and without @ in the parameter name in the SqlParameter
object as well.
Both the build methods just use reflection to build the string and list based on the name and values of the object passed into the method, if needed I will supply this code as well.
I got this working by modifying the call to the stored procedure from an comment provided by @galenus
public string PlaceOrder(IPOPPlaceOrderRequest order)
{
try
{
string storedProcedure = BuildSQLExecutionString<IPOPPlaceOrderRequest>(order, "exec @RetVal = Web_NewOrder");
List<SqlParameter> parameters = BuildSQLParameters<IPOPPlaceOrderRequest>(order);
var retVal = new SqlParameter("@RetVal", System.Data.SqlDbType.Int) { Direction = System.Data.ParameterDirection.Output, DbType = System.Data.DbType.Int32};
parameters.Add(retVal);
return tOrderRepository.RunSqlStoredProcedureSingle<object>(storedProcedure, parameters.ToArray());
}
catch (Exception ex)
{
throw ex;
}
}
By adding a declaration for the return parameter in the storedProcedure
string it now returns the correct value.
Here's the question and answer which I got this information from: Get return value from stored procedure
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.