我有一个表格,偶尔导入时会在其中切换贷方和借方-因此,我有一些代码来设置变量,然后在变量匹配时进行交换,但是我无法让它在IF中运行语句,我试图将其放入存储过程中,但也无法在IF语句中运行该存储过程。你会推荐什么?参见下面的代码。谢谢
set @TotalCr = (select max(TotalCredit) from mbs_headers);
If (@TotalCr = (select sum(PDebit) from mbs_details) and @TotalCr =! (select sum(PCredit) from mbs_details))
then UPDATE mbs_detailstest SET PCredit=@tmp:=PCredit, PCredit=PDebit, PDebit=@tmp
else
end;
OR
set @TotalCr = (select max(TotalCredit) from mbs_headers);
If (@TotalCr = (select sum(PDebit) from mbs_details) and @TotalCr =! (select sum(PCredit) from mbs_details))
then exec TestSwapProc
else
end;
仅包含TestSwapProc
UPDATE mbs_detailstest SET PCredit=@tmp:=PCredit, PCredit=PDebit, PDebit=@tmp
样本数据- 表格:详细信息
Name, Credit, Debit
Mike, 100, 20
Mike, 10, 0
表:标题
name, totalcredits, totaldebits
Mike, 20, 110
我需要切换Details表以反映真实值,因为有时它会以错误的方式插入 预期的结果:
表:详细信息
Name, Credits, Debits
Mike, 20, 100
Mike, 0, 10
标题表保持不变,因为它总是正确的
答案 0 :(得分:0)
You can do something like this:
declare v_total decimal(10,2);
declare v_debit decimal(10,2);
declare v_credit decimal(10,2);
select max(TotalCredit) into v_total
from mbs_headers;
select sum(PDebit), sum(PCredit) into v_debit, v_credit
from mbs_details;
If (v_total = v_debit and v_total != v_credit) then
UPDATE mbs_detailstest
SET PCredit=v_debit, PDebit=v_credit;
end if;