【问题标题】:Oracle. How can I manipulate variables and actions in procedures based on input-argument甲骨文。如何根据输入参数在过程中操作变量和动作
【发布时间】:2015-09-01 03:06:20
【问题描述】:

首先,我要感谢 StackOwerflow 社区的每一个人。这个页面和它的贡献者是并且是信息和知识的源泉,它在许多可怕的情况下满足了我的渴望。

我也为这篇文章的篇幅道歉,我只想说清楚。

我在 Oracle(10g 版本 10.2.0.5.0)数据库中有一个表,工作人员通过 php-jquery Web 应用程序将过滤器(大圆柱形袋)的状态记录到该表中。

这个表叫做 Filter_Bag_entries 有以下列

ID || EMPLOYEE_ID || THEDATE || FILTER_NO || BIN_NO || ACTION || FILTER_BAG_ID || INSERT_DATE || PERIOD

相关的列是 Filter_NO 是过滤器的编号(1-3),Bin_no 是每个过滤器中的箱数(1-10),Filter_bag_id 是每个箱中每个过滤器的 ID(A1 -20:F1-20),Period,即 bin 当前的使用寿命(每个 bin 中的所有袋子在 X 年后更换,期间编号然后增加 1),最后是最后一个相关列 Action,即接受单个字母,例如 B(表示失明,即损坏的包)、E(表示更新(冰岛语))、U、S...等。

日志运行良好,一切正常。

不过,我需要的是按需轻松计数每个垃圾箱中的盲袋。对于当前的表,这可能有点棘手,因为袋子可以被蒙蔽、更新、再次蒙蔽等等。所以简单地用 action=B 计算 Bag_id 是不够的。这可以通过 php 例程来完成,但目前我在日志表中有很多数据,我想要一个好的、可靠的方法。

我的想法是使用另一个表 Blinded_Bags,它具有以下列。

FILTER_NO || BIN_NO || IS_BLIND || FILTER_BAG_ID || PERIOD

如果袋子已被遮盖,ISBLIND 列的值为 1,如果袋子被更新,则值为 0。我将对 Period、Filter_NO、Bin_NO 和 Filter_bag_id 设置唯一的约束,因此每个周期的每个过滤器的每个 bin 中的每个袋子只能存在一个条目。

然后我想使用执行以下操作的存储过程:

Worker在Bin=1,Filter=1中选择Bag_id=B15,在web-application中选择“Blind”并提交到数据库。

数据库将条目输入到日志表中,就像现在一样。然后发生另一件事(通过程序),只有当动作是“E”或“B”时才会发生。检查动作(在本例中为 B),如果是“B”,则触发变量 var_isblind=1。该过程执行以下操作:

INSERT INTO BLINDED_BAGS(FILTER_NO, BIN_NO, PERIOD, FILTER_BAG_ID, ISBLIND) VALUES(1, 1, (select max(period) from filter_bag_entries where filter_no=1,bin_no=1), var_isblind)

如果触发了约束错误,则意味着当前包有一个条目(推测 ISBLIND 的值为 0),然后该过程会更新表。

现在,我知道如何创建一个过程,我大概也可以弄清楚如何将一个过程插入到多个表中,我还知道如何处理异常以在唯一约束失败时选择 beteen 更新和插入。 我不知道的是如何操作变量。也就是说,我不知道我如何在oracle中执行以下操作

if action == 'E'

    var_isblind = 0; // and contintue to insert or update into BLINDED_BAGS

else if action == 'B'

    var_isblind = 1; // and contintue to insert or update into BLINDED_BAGS

else

    Return false; // Exit procedure and don‘t do anything to BLINDED_BAGS

【问题讨论】:

  • 我对你的逻辑并不完全清楚,对我来说,我会尝试在你的初始表中获取最后一个条目作为盲袋(以避免重复计算),也许尝试创建一个视图而不是另一个表。但是假设你必须沿着程序和第二个表路由,我会查看 BEFORE INSERT/UPDATE 触发器以更改一个值,然后需要将其插入到你的第二个表中
  • 你可能是对的。这个视图听起来确实很吸引人,实际上我在几秒钟前创建了一个似乎做了我不想做的事情。我只是发现另一种方法(两张桌子)更美观,但到目前为止,它看起来确实很傻......
  • 如果我是你,我会在这里查看物化视图,其中只有那些更新的记录才会被修改。

标签: oracle stored-procedures oracle10g conditional-statements stored-functions


【解决方案1】:

请在下面找到不言自明的代码 在这里你可以找到工作的example

Oracle中有一个“UPSERT”语句,叫做MERGE

我在触发器中使用了它

create table filter_bag_entries (
  id            int     not null,
  employee_id   int,
  thedate       date,
  filter_no     int,
  bin_no        int,
  action        varchar2(3),
  filter_bag_id int,
  insert_date   date    default sysdate,
  period        int,
  constraint pk_filter_bag_entries primary key(id)
);


create table blinded_bags (
  filter_no     int,
  bin_no        int,
  is_blind      int,
  filter_bag_id int,
  period        int,
  constraint pk_blinded_bags primary key (filter_bag_id, period, filter_no, bin_no)
);


create or replace trigger tr_flt_bag_entr
after insert or update
on filter_bag_entries
for each row
when (new.action in ('E','B'))
declare
    l_isblind   int;
begin
    l_isblind := case :new.action
                    when 'E' then 0
                    when 'B' then 1
                 end;

    merge into blinded_bags tgt
    using ( select :new.filter_no as filter_no, :new.bin_no as bin_no, l_isblind as is_blind,
            :new.filter_bag_id as filter_bag_id, :new.period as period from dual
    ) src
    on (src.filter_no = tgt.filter_no and src.bin_no = tgt.bin_no
      and src.filter_bag_id = tgt.filter_bag_id and src.period = tgt.period
    )
    when matched then
        update  set tgt.is_blind = l_isblind
    when NOT matched then
        insert values (src.filter_no, src.bin_no, src.is_blind, src.filter_bag_id, src.period);
end;
/
show err

truncate table filter_bag_entries;

insert into filter_bag_entries
    (id, employee_id, thedate, filter_no, bin_no, action, filter_bag_id, period)
values (1, 100, sysdate, 1, 1, 'B', 31, 10);

insert into filter_bag_entries
    (id, employee_id, thedate, filter_no, bin_no, action, filter_bag_id, period)
values (2, 100, sysdate, 2, 2, 'B', 32, 10);

insert into filter_bag_entries
    (id, employee_id, thedate, filter_no, bin_no, action, filter_bag_id, period)
values (3, 100, sysdate, 3, 3, 'E', 33, 10);

insert into filter_bag_entries
    (id, employee_id, thedate, filter_no, bin_no, action, filter_bag_id, period)
values (4, 101, sysdate, 3, 3, 'E', 33, 11);

commit;

select * from blinded_bags;


update filter_bag_entries
  set action = 'B'
where filter_no=3 and bin_no=3 and filter_bag_id=33 and period=10 and action!='B';

select * from blinded_bags;

输出:

SQL> select * from blinded_bags;

 FILTER_NO     BIN_NO   IS_BLIND FILTER_BAG_ID     PERIOD
---------- ---------- ---------- ------------- ----------
         1          1          1            31         10
         2          2          1            32         10
         3          3          0            33         10
         3          3          0            33         11

SQL> update filter_bag_entries
  set action = 'B'
where filter_no=3 and bin_no=3 and filter_bag_id=33 and period=10 and action!='B';
  2    3
1 row updated.

SQL> select * from blinded_bags;

 FILTER_NO     BIN_NO   IS_BLIND FILTER_BAG_ID     PERIOD
---------- ---------- ---------- ------------- ----------
         1          1          1            31         10
         2          2          1            32         10
         3          3          1            33         10
         3          3          0            33         11

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 2022-12-05
    相关资源
    最近更新 更多