0

10 進数値を返さなければならない clr ストアド プロシージャがあります。現時点では出力パラメータを使用できません。出力パラメータを処理しない既存のライブラリを再利用する必要があります。ストアド プロシージャは値を返しますが、10 進数が失われます。
理由はありますか?ありがとう、ジェニー

[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 に答える