我在这里有一个情况:
当特定列说procedure_1
中有数据时,我们需要在包中运行X
,如果该列中没有数据{ {1}}
任何人都可以就使用Oracle可以做什么提出建议吗?
答案 0 :(得分:0)
像这样吗?
DECIDE
包含PARTICULAR_COLUMN
。
SQL> create table decide
2 (id number,
3 particular_column varchar2(1));
Table created.
SQL> insert into decide values (1, 'X');
1 row created.
SQL> insert into decide values (2, null);
1 row created.
SQL> create or replace package pkg_decide as
2 procedure p1;
3 procedure p2;
4 end;
5 /
Package created.
SQL> create or replace package body pkg_decide as
2 procedure p1 is
3 begin
4 dbms_output.put_line('-> running proc 1');
5 end;
6
7 procedure p2 is
8 begin
9 dbms_output.put_line('-> running proc 2');
10 end;
11 end;
12 /
Package body created.
SQL>
测试:如果PARTICULAR_COLUMN
不为空,则P1
将运行;否则为P2
。这可以通过动态SQL(即EXECUTE IMMEDIATE
)来完成。在运行过程时,我们必须形成一个完整的匿名PL / SQL块(开始-过程名称-结束)。
P.S。正如马修(Matthew)所建议的,一个简单的IF就可以。
SQL> begin
2 for cur_r in (select particular_column,
3 case when particular_column is not null then 'pkg_decide.p1'
4 else 'pkg_decide.p2'
5 end prc_name
6 from decide
7 ) loop
8 dbms_output.put_line('particular column = ' || cur_r.particular_column ||
9 ', should run procedure ' || cur_r.prc_name);
10 execute immediate 'begin ' ||cur_r.prc_name ||'; end;';
11
12 -- Or, as suggested by Matthew, a simple IF will do
13 if cur_r.particular_column is not null then
14 pkg_decide.p1;
15 else
16 pkg_decide.p2;
17 end if;
18 end loop;
19 end;
20 /
particular column = X, should run procedure pkg_decide.p1
-> running proc 1
-> running proc 1
particular column = , should run procedure pkg_decide.p2
-> running proc 2
-> running proc 2
PL/SQL procedure successfully completed.
SQL>