为什么我的查询没有使用我的输入参数更新表

本文关键字:我的 输入 参数 更新 查询 为什么 | 更新日期: 2023-09-27 18:34:39

我不知道

为什么以下查询没有使用我的预期参数执行!!

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 = 1,虽然currentcredit = 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;
            }

为什么我的查询没有使用我的输入参数更新表

从评论:

当前信用在更新前为空,我该怎么办

啊,这就是问题所在。在 SQL 中,null是粘性的。 null+任何东西都是:null .如果这是 TSQL(即服务器.SQL(,解决方案将是ISNULL

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

其中,如果x为非空,则ISNULL(x, y)的结果x,否则y .在C#术语中,它相当于x ?? y(实际上,ISNULL(x, y)COALESCE(x, y)相同,只是COALESCE无序的(。

所以:找到 ISNULLCOALESCE 等效的 informix ,并使用它

从简短的搜索来看,似乎在 informix 中,NVL 函数是这样做的,所以请尝试:

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