In the following code when I try to update amount in the table t_payment, I expect it to be set the value of Convert.ToInt32(request.amount+ previous_paid_amount);
whereas previous_paid_amount
is assumed to be 0
instead of getting updated. So I am unable to use the updated value of previous_paid_amount
variable.
Any help would be highly appreciated. Thanks
double previous_paid_amount = 0;
try {
OracleCommand command2 = new OracleCommand();
command2.CommandText = "select amount from t_payment where penalty_order_id = (select id from t_penalty_order where protokol_no = :invoiceNumber)";
command2.Parameters.Add(new OracleParameter(@"invoiceNumber", OracleDbType.Varchar2, 255)).Value = request.invoiceNumber;
command2.Connection = connection;
command2.CommandType = System.Data.CommandType.Text;
using (OracleDataReader row2 = command.ExecuteReader()) {
while (row2.Read()) {
previous_paid_amount = Convert.ToInt32(row2.GetValue(0));
}
}
}
catch (Exception e) {
completePayment.code = 111;
completePayment.message = e.Message;
completePayment.transactionNumber = null;
}
// update the paid amount by adding the current amount
try {
OracleCommand command2 = new OracleCommand();
command2.CommandText = "Update t_payment set amount = :amount where penalty_order_id = (select id from t_penalty_order where protokol_no = :invoiceNumber)";
command2.Parameters.Add(new OracleParameter(@"amount", OracleDbType.Int32)).Value = Convert.ToInt32(request.amount+ previous_paid_amount);
command2.Parameters.Add(new OracleParameter(@"invoiceNumber", OracleDbType.Varchar2, 255)).Value = request.invoiceNumber;
command2.Connection = connection;
command2.CommandType = System.Data.CommandType.Text;
command2.ExecuteNonQuery();
}
catch (Exception e) {
completePayment.code = 111;
completePayment.message = e.Message;
completePayment.transactionNumber = null;
}
I suggest executing just one query (we don't want to do extra work by querying Oracle twice , fetching data to the workstation then pushing at again to Oracle). As far as I can see from your
"Update t_payment set amount = :amount where penalty_order_id = (select id from t_penalty_order where protokol_no = :invoiceNumber)";
code, you want to update t_payment
table by adding some extra money to amount
field with condition applied:
update t_payment
set amount = amount + SOME_EXTRA_MONEY
where penalty_order_id in (select p.id
from t_penalty_order p
where p.protokol_no = :invoiceNumber)
We have to determine what is SOME_EXTRA_MONEY
: we can try to derive it from query
"select amount from t_payment where penalty_order_id = (select id from t_penalty_order where protokol_no = :invoiceNumber)";
So we have (I've added Sum
in case we have several records, and that's why should sum them and Nvl
in case we have none - in this case extra sum is 0
):
update t_payment
set amount = amount + (select Nvl(Sum(t.amount), 0)
from t_payment t
where t.penalty_order_id in (select p.id
from t_penalty_order p
where p.protokol_no = :invoiceNumber))
where penalty_order_id in (select p.id
from t_penalty_order p
where p.protokol_no = :invoiceNumber)
Time to execute this query:
using (OracleCommand command2 = new OracleCommand()) {
command2.CommandText =
@"update t_payment
set amount = amount + (select Nvl(Sum(t.amount), 0)
from t_payment t
where t.penalty_order_id in (select p.id
from t_penalty_order p
where p.protokol_no = :invoiceNumber))
where penalty_order_id in (select p.id
from t_penalty_order p
where p.protokol_no = :invoiceNumber)";
command2.Parameters.Add(
new OracleParameter(@"invoiceNumber", OracleDbType.Varchar2, 255)
).Value = request.invoiceNumber;
command2.ExecuteNonQuery();
}
It didn't work because you are using command instead of command2.
using (OracleDataReader row2 = command.ExecuteReader())
This needs to be modified with
using (OracleDataReader row2 = command2.ExecuteReader())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.