0

我有一个必须返回十进制值的 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(record); 不返回小数位。

4

1 回答 1

2

默认小数的精度/比例为 18/0。尝试使用不同的重载来创建元数据:

SqlDataRecord record = new SqlDataRecord(new SqlMetaData("decValue", SqlDbType.Decimal, 19, 9, false, false, SortOrder.Ascending, 0));
于 2011-09-22T21:07:26.677 回答