我有一个必须返回十进制值的clr存储过程。我此时无法使用输出参数 - 我需要重用不处理输出参数的现有库。 我的存储过程返回一个值 - 但它会丢失十进制数字 有什么想法吗? 谢谢, 珍妮
[SqlProcedure]
public static int CalculateMyValue(SqlDecimal aInput1, SqlString 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;
}
}
编辑:忘了添加SqlContext.Pipe.Send(string.Format(“decValue = {0}”,decValue)); 显示小数位但SqlContext.Pipe.Send(记录);不返回小数位。
答案 0 :(得分:2)
默认小数精度/小数位数是18/0。尝试使用不同的重载来创建元数据:
SqlDataRecord record = new SqlDataRecord(new SqlMetaData("decValue", SqlDbType.Decimal, 19, 9, false, false, SortOrder.Ascending, 0));