[英]How to convert string value returned from oracle apex 20.1 multiselect item into comma separated numbers array
I have a multi select enabled select list.我有一个多 select 启用 select 列表。 I want to use all the selected ids inside an IN () operator in pl/sql query.
我想在 pl/sql 查询中使用 IN () 运算符中的所有选定 ID。 Selected values are returned as below,
选择的值返回如下,
"1","5","4"
I want to use em as numbers as below,我想用 em 作为数字,如下所示,
1,5,4
My query is like,我的查询就像,
UPDATE EMPLOYEE SET EMPSTAT = 'Active' WHERE EMPID IN (:P500_EMPIDS);
You can use the API apex_string for this.您可以为此使用 API apex_string 。 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.
如果要使用 IN 运算符,则必须使用 EXECUTE IMMEDIATE,因为不能在 IN 运算符中使用串联字符串。 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.说明:将冒号分隔的 id 列表转换为 varchar2 表,然后循环遍历该表的元素。
Note that I'm using ":" as a separator, that is what apex uses for multi selects.请注意,我使用“:”作为分隔符,这就是 apex 用于多选的内容。 If you need "," then change code above accordingly.
如果您需要“,”,则相应地更改上面的代码。
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:这是一种将逗号分隔的值拆分为行(而不是您将在
IN
中使用的值列表)的方法:请注意:
REPLACE
function replaces double quotes with an empty stringREPLACE
function 用空字符串替换双引号REGEXP_SUBSTR
with help of hierarchical queryREGEXP_SUBSTR
将其拆分为行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, eg 1:5:4
.通常多选项目具有冒号分隔的值,例如
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:在 Apex 中使用它作为:
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.试试看。
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;
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:请注意,您可以直接在更新语句中使用
apex_string
,因此Koen Lostrie
的答案可以修改为不需要循环:
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),','))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.