How to convert string value returned from oracle apex 20.1 multiselect item into comma separated numbers array

泪湿孤枕 提交于 2020-07-10 10:29:05

问题


I have a multi select enabled select list. I want to use all the selected ids inside an IN () operator in pl/sql query. Selected values are returned as below,

"1","5","4"

I want to use em as numbers as below,

1,5,4

My query is like,

UPDATE EMPLOYEE SET EMPSTAT = 'Active' WHERE EMPID IN (:P500_EMPIDS);

回答1:


This is the employee table:

SQL> select * from employee;

     EMPID EMPSTAT
---------- --------
         1 Inactive
         2 Inactive
         4 Inactive
         5 Inactive

SQL>

This is a way to split comma-separated values into rows (not into a list of values you'd use in IN!). Note that:

  • line #3: REPLACE function replaces double quotes with an empty string
  • line #3: then it is split into rows using REGEXP_SUBSTR with help of hierarchical query

SQL> with test (col) as
  2    (select '"1","5","4"' from dual)
  3  select regexp_substr(replace(col, '"', ''), '[^,]+', 1, level) val
  4  from test
  5  connect by level <= regexp_count(col, ',') + 1;

VAL
--------------------
1
5
4

SQL>

Usually multiselect items have colon-separated values, e.g. 1:5:4. If that's really the case, regular expression would look like this:

regexp_substr(col, '[^:]+', 1, level) val

Use it in Apex as:

update employee e set
  e.empstat = 'Active'
  where e.empid in 
    (select regexp_substr(replace(:P1_ITEM, '"', ''), '[^,]+', 1, level)
     from dual
     connect by level <= regexp_count(:P1_ITEM, ',') + 1
    );

Result is:

3 rows updated.

SQL> select * from employee order by empid;

     EMPID EMPSTAT
---------- --------
         1 Active
         2 Inactive
         4 Active
         5 Active

SQL>

Try it.




回答2:


Thanks for helping everyone.Please check this and tell me if anything is wrong. I found a solution as below,

DECLARE
l_selected APEX_APPLICATION_GLOBAL.VC_ARR2;
BEGIN
    l_selected := APEX_UTIL.STRING_TO_TABLE(:P500_EMPIDS);
    FOR i in 1 .. l_selected.count LOOP 
        UPDATE EMPLYEE SET EMPSTATUS = 'ACTIVE' WHERE EMPID = to_number(l_selected(i));
    END LOOP;
END;



回答3:


You can use the API apex_string for this. If you want to use the IN operator you'll have to use EXECUTE IMMEDIATE because you cannot use a concatenated string in an IN operator. Instead what you could do is the following:

DECLARE
  l_array           apex_t_varchar2;
BEGIN
  l_array := apex_string.split(p_str  => :P500_EMPIDS, p_sep  => ':');
   FOR i IN 1..l_array.count LOOP
     UPDATE EMPLOYEE SET EMPSTAT = 'Active' WHERE EMPID = l_array(i);
  END LOOP;
END;

Explanation: convert the colon separated list of ids to a table of varchar2, then loop through the elements of that table.

Note that I'm using ":" as a separator, that is what apex uses for multi selects. If you need "," then change code above accordingly.




回答4:


Note that you can use apex_string directly within an update statement, so the answer of Koen Lostrie could be modified to not need a loop:

UPDATE EMPLOYEE
SET EMPSTAT = 'Active'
WHERE EMPID IN (
    select to_number(trim('"' from column_value))
    from table(apex_string.split(:P500_EMPIDS,','))
);

Testcase:

with cte1 as (
    select '"1","2","3"' as x from dual
)
select to_number(trim('"' from column_value))
from table(apex_string.split((select x from cte1),','))


来源:https://stackoverflow.com/questions/62534753/how-to-convert-string-value-returned-from-oracle-apex-20-1-multiselect-item-into

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!