简体   繁体   English

如何将从 oracle apex 20.1 多选项目返回的字符串值转换为逗号分隔的数字数组

[英]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中使用的值列表)的方法:请注意:

  • line #3: REPLACE function replaces double quotes with an empty string第 3 行: REPLACE function 用空字符串替换双引号
  • line #3: then it is split into rows using REGEXP_SUBSTR with help of hierarchical query第 3 行:然后在分层查询的帮助下使用REGEXP_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.

相关问题 如何在oracle中从逗号分隔的字符串中删除特定值 - How to remove specific value from comma separated string in oracle 如何根据项目创建 URL 并保存以供复制到 Oracle Apex 20.1 - How to create a URL based on Item and save if afterwards for copy in Oracle Apex 20.1 如何将逗号分隔的字符串转换为逗号分隔的整数并将其作为参数传递给 Oracle? - How to convert a comma separated string to comma separated integers and pass it as parameters in Oracle? 列中的Oracle逗号分隔值(ID)。 如何获取逗号分隔字符串中每个值的描述。 - Oracle Comma Separated Value (ID) in a Column. How to get Description for each Value in a Comma Separated string. 如何将列中的逗号分隔值与Oracle中的字符串集合进行比较 - How to compare a comma separated value in a column with a string collection in Oracle 如何使用 oracle 数据库 10g 安装 oracle APEX 20.1 - How to install oracle APEX 20.1 with oracle database 10g 如何将逗号分隔值转换为 oracle 中的行? - How to convert comma separated values to rows in oracle? 在 Oracle 19.3 上安装 APEX 20.1 - Installing APEX 20.1 on Oracle 19.3 如何在PL / SQL中将逗号分隔的负值转换为数组? - How to convert comma separated negative value to array in PL/SQL? Oracle Apex 20.1自动化测试 - Oracle Apex 20.1 automated testing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM