0

次のクエリが予期したパラメーターで実行されない理由がわかりません!!

cmdTxt.Append("UPDATE sd32depart SET currentcredit = currentcredit + ? WHERE year = ? AND main_code = ? ");
paramList.Add("currentcredit", value.ToString().TrimEnd());
paramList.Add("year", year.ToString().TrimEnd());
paramList.Add("main_code", main_code.ToString().TrimEnd());
res = ConnectionObj.Execute_NonQueryWithTransaction(cmdTxt.ToString(), CommandType.Text, paramList);

私は得る

res = 1currentcredit = 180パラメータとして

テーブルをチェックすると、見つけましたcurrentcredit NULL!!


public int Execute_NonQueryWithTransaction(string cmdText)
            {
                string return_msg = "";
                int return_val = -1;
                //check if connection closed then return -1;
                if (connectionstate == ConnectionState.Closed)
                    return -1;
                command.CommandText = cmdText;
                command.CommandType = CommandType.Text;
                command.Transaction = current_trans;
                try
                {
                    return_val = command.ExecuteNonQuery();
                }
                catch (IfxException ifxEx)// Handle IBM.data.informix : mostly catched
                {
                    return_val = ifxEx.Errors[0].NativeError;
                    return_msg = return_val.ToString();
                }
                catch (Exception ex)// Handle all other exceptions.
                {
                    return_msg = ex.Message;
                }
                finally
                {
                    if (!string.IsNullOrEmpty(return_msg))//catch error
                    {
                        //rollback
                        current_trans.Rollback();
                        Close_Connection();
                    }

                }
                return return_val;
            }
4

1 に答える 1

1

コメントから:

currentcredit が更新前に null になっています。どうすればよいですか

ああ、それでは問題です。SQL では、nullsticky です。null+ 何か: null. これが TSQL (つまり SQL Server) の場合、解決策は次のようになりますISNULL

UPDATE sd32depart SET currentcredit = ISNULL(currentcredit,0) + ?

ここで、 ifの結果はISNULL(x, y)null以外であり、それ以外の場合はです。C# の用語では、これは と同等です(実際、is varadicを除いてと同じです)。xxyx ?? yISNULL(x, y)COALESCE(x, y)COALESCE

ISNULLそのため、 orに相当する informix を見つけてCOALESCE、それを使用します。

簡単な検索から、informixではNVL関数がこれを行うように思われるので、次を試してください。

UPDATE sd32depart SET currentcredit = NVL(currentcredit,0) + ?
于 2013-10-10T09:40:25.227 に答える