Pl sql-读数学公式并用数字代替

问题描述 投票:0回答:1

我有以下表格:

TABLE  1                                                         TABLE 2

Col 1        Col2                                              A      $100
 Rpt1     (A + B(1-D))/C                                       B      $200
 Rpt2         -A                                               C      $300
 Rpt3         C+D                                              D      $400

我想编写一个PL-SQL代码来读取这两个表并动态地给我下面的代码。

Rpt 1  =  (100 +200(1-400))/300
Rpt 2  =  - 100
Rpt 3  =  300+400

表1可以具有任何类型的配方。我需要阅读该公式并将其替换为表2中的金额。

有什么想法吗?

sql oracle11g
1个回答
3
投票

既然你要求PL / SQL,这里有一个嵌套循环解决方案:

create table table1 (col1, col2) as
select 'Rpt1', '(A + B(1-D))/C' from dual union all
select 'Rpt2', '-A' from dual union all
select 'Rpt3', 'C+D' from dual;

create table table2 (col1, col2) as
select 'A', 100 from dual union all
select 'B', 200 from dual union all
select 'C', 300 from dual union all
select 'D', 400 from dual;

set serveroutput on

declare
  result varchar2(100);
begin
  for r1 in (select col1, col2 from table1)
  loop
    result := r1.col2;
    for r2 in (select col1, col2 from table2 where result like '%' || col1 || '%')
    loop
      result := replace(result, r2.col1, r2.col2);
    end loop;
    dbms_output.put_line(r1.col1 || '  =  ' || result);
  end loop;
end;
/

Rpt1  =  (100 + 200(1-400))/300
Rpt2  =  -100
Rpt3  =  300+400


PL/SQL procedure successfully completed.

我假设美元符号实际上并不存在于table2;如果他们是那么你需要修剪它们如果你不想在输出中。

© www.soinside.com 2019 - 2024. All rights reserved.