clr stored procedure with SqlContext.Pipe.Send should return decimal, returns int
I have a clr stored procedure that has to return decimal value. I cannot use output parameters at this moment - i need to reuse the existing library that doesn't process output parameters. My stored procedure returns a value - but it loosed decimal digits.
Any ideas why? Thanks, Jenny[SqlProcedure]
public static int CalculateMyValue(SqlDecimal aInput1, SqlSt开发者_C百科ring aInput2, SqlDecimal aInput3)
{
try
{
//convert second value to code. call regular stored procedure
SqlConnection conn = new SqlConnection("context connection=true");
SqlCommand cmd = new SqlCommand("sp_getCodeForVal", conn);
cmd.Parameters.AddWithValue("@MyParam", aInput2);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
object input2CodeSql = cmd.ExecuteScalar();
conn.Close();
if (input2CodeSql == null)
{
// debug print
SqlContext.Pipe.Send(string.Format("aInput2 = {0}", aInput2));
return 2;
}
decimal input2Code = Convert.ToDecimal(input2CodeSql.ToString());
decimal input1Val = aInput1.Value;
decimal input3Val = aInput3.Value;
GetMyDecimal myVal = new GetMyDecimal();
decimal decValue = myVal.Calculate(input1Val, input2Code, input3Val);
//debug
SqlContext.Pipe.Send(string.Format("decValue = {0}", decValue));
// Create a record object that represents an individual row, including it's metadata.
SqlDataRecord record = new SqlDataRecord(new SqlMetaData("decValue", SqlDbType.Decimal));
// Populate the record.
record.SetDecimal(0, decValue);
// Send the record to the client.
SqlContext.Pipe.Send(record);
return 0;
}
catch (Exception)
{
return 2;
}
}
EDIT: forgot to add that SqlContext.Pipe.Send(string.Format("decValue = {0}", decValue)); shows decimal places but SqlContext.Pipe.Send(record); returns no decimal places.
Default decimal's precicion/scale is 18/0. Try using different overload for creating Meta Data:
SqlDataRecord record = new SqlDataRecord(new SqlMetaData("decValue", SqlDbType.Decimal, 19, 9, false, false, SortOrder.Ascending, 0));
精彩评论