ORACLE顶点-使用PL / SQL遍历复选框项

时间:2018-10-18 15:25:07

标签: oracle plsql oracle-apex oracle-apex-5.1

我的页面P3_Checkbox1上有一个从数据库填充的复选框。如何在PL / SQL代码中遍历复选框的所有选中值?

我假设APEX_APPLICATION.G_F01仅由sql生成的复选框使用,并且我不能将其用于常规checbox,因为它不会填充G_Fxx数组。

1 个答案:

答案 0 :(得分:1)

我想我现在明白您的意思了。

例如,假设P3_CHECKBOX1复选框项允许3个值(显示/返回):

  • 缺席:-1
  • 未知:0
  • 这里:1

我创建了一个按钮(将仅SUBMIT页)和两个文本项,这些文本项将显示选中的值:P3_CHECKED_VALUESP3_CHECKED_VALUES_2

然后我创建了一个进程,当按下按钮时会触发该进程。该过程如下所示(请同时阅读评论):

begin
  -- This is trivial; checked (selected) values are separated by a colon sign, 
  -- just like in a Shuttle item
  :P3_CHECKED_VALUES := :P3_CHECKBOX1;

  -- This is what you might be looking for; it uses the APEX_STRING.SPLIT function
  -- which splits selected values (the result is ROWS, not a column), and these
  -- values can then be used in a join or anywhere else, as if it was result of a
  -- subquery. The TABLE function is used as well.
  -- LISTAGG is used just to return a single value so that I wouldn't have to worry
  -- about TOO-MANY-ROWS error.
  with 
  description (code, descr) as
    (select -1, 'Absent'  from dual union all
     select 0 , 'Unknown' from dual union all
     select 1 , 'Here'    from dual),
  desc_join as
    (select d.descr
     from description d join (select * from table(apex_string.split(:P3_CHECKED_VALUES, ':'))) s
     on d.code = s.column_value
    )
  select listagg(j.descr, ' / ') within group (order by null) 
    into :P3_CHECKED_VALUES_2
  from desc_join j;
end;

假设已检查“缺勤”和“未知”值。该PL / SQL过程的结果是:

P3_CHECKED_VALUES   = -1:0
P3_CHECKED_VALUES_2 = Absent / Unknown

您可以根据需要重写它;这只是一个例子。

关键字:

  • APEX_STRING.SPLIT
  • TABLE函数

希望对您有帮助。

[编辑:遍历所选值]

遍历这些值并不困难;您可以按照以下步骤进行操作(请参见光标FOR循环):

declare
  l_dummy number;
begin
  for cur_r in (select * From table(apex_string.split(:P3_CHECKED_VALUES, ':'))) 
  loop
    select max(1)
      into l_dummy
      from some_table where some_column = cur_r.column_value;

    if l_dummy is null then
       -- checked value does not exist
       insert into some_table (some_column, ...) valued (cur_r.column_value, ...);
    else
       -- checked value exists
       delete from some_table where ...
    end if;
  end loop;
end;

但是,我不确定您说“数据库中存在特定组合”是什么意思。这是否意味着您要将冒号分隔的值存储到该表的列中?如果是这样,则上面的代码也不起作用,因为您进行了比较,例如

  • -10:-1,然后
  • 00:-1

这不是真的(在最简单的情况下,当检查和存储的值只有一个值时除外)。

尽管Apex的“多项选择”看起来不错,但当您必须实际与他们进行某些事情时(例如您的情况),它们可能会成为噩梦。

也许您应该先 sort 复选框值,对数据库值进行排序,然后比较这两个字符串。

这意味着LISTAGG可能再次变得方便,例如

listagg(j.descr, ' / ') within group (order by j.descr) 

数据库值可以按以下方式排序:

SQL> with test (col) as
  2    (select '1:0' from dual union all
  3     select '1:-1:0' from dual
  4    ),
  5  inter as
  6    (select col,
  7            regexp_substr(col, '[^:]+', 1, column_value) token
  8     from test,
  9          table(cast(multiset(select level from dual
 10                              connect by level <= regexp_count(col, ':') + 1
 11                             ) as sys.odcinumberlist))
 12    )
 13  select
 14    col source_value,
 15    listagg(token, ':') within group (order by token) sorted_value
 16  from inter
 17  group by col;

SOURCE SORTED_VALUE
------ --------------------
1:-1:0 -1:0:1
1:0    0:1

SQL>

将它们都排序后,就可以比较它们,并INSERTDELETE行进行比较。