how to return decimal value from stored procedure
The return value can only be an integer.
Use an output parameter to get the result from the procedure:
ALTER PROCEDURE [dbo].[sp_UpdateStockForSale]
@prodName varchar(40),
@stqty numeric(9,2),
@batchno varchar(40),
@IsSample varchar(5),
@NetRate decimal(9,2) output
AS
...
Call it using:
declare @Result decimal(9,2)
exec
[dbo].[sp_UpdateStockForSale]
@prodName = N'vicks',
@stqty = 30,
@batchno = N'v7',
@IsSample = N'false',
@NetRate = @Result output
In C# you would create an output parameter and add to the parameters before the call:
SqlParameter result = new SqlParameter("@NetRate", SqlDbType.Decimal);
result.Direction = ParameterDirection.Output;
command.Parameters.Add(result);
After the call (and reading the result set if there is one) you can get the value from the parameter:
Decimal netRate = (Decimal)result.Value;
I know, It's too late to give answer. As Guffa has already suggested a good solution and it will return correct result as you want.
But, still there is another solution which i would like to share with you that.
use select statement at the end of you stored procedure instead of return statement.
Update STOCK Set S_P_ttavail = @ttavail, S_P_ttsold=@ttsold Where S_en=@S_en And IsSample=@IsSample
END
Select @NetRate As NetRate
END
and execute your stored procedure by using ExecuteScalar()
method. How you can avoid unnecessary parameter declaration in you stored procedure.
var dNetRate = cmd.ExecuteScalar();
but, still i would like to suggest you to use output parameter if you have no problem in creating another parameter in your stored procedure otherwise you can stick with this solution.