0

我有一个案例,在 2 个输入变量中,我将传递逗号分隔的电子邮件和用户 ID,或者其中一个或一个都不传递

现在的事情是程序将具有AND条件,如果电子邮件存在,它应该应用于过滤表的电子邮件列,对于用户ID也是如此

并且情况是数据库中的电子邮件和用户ID的值也为“null”,因此在nvl中,如果我们发送null,那么它将返回所有值,即使是那些持有null的值,然后它将采取“其中userid为null” "

例子:

userid.             name
null.                  xyz
abc.                  null
adj.                   kak

例子 :

procedurename(phone in number, name in varchar2, userid in varchar2, cursor_c out sys_refcursor) is

begin

open cursor_c for
select name, email,mobile,phone, address, department,grade,scale

from employee where 

user_id =nvl(select regexpr_str(userid,'[^,]+',1,level) from dual connect by regexpr_str(userid,'[^,]+',1,level) is not null),user_id))

and name=nvl(select regexpr_str(name,'[^,]+',1,level) from dual connect by regexpr_str(name,'[^,]+',1,level) is not null),name))

因此,如果我们将 NULL 作为参数传递,那么它甚至会选择“name=null”的行

不应该这样做,因为它会选择那些 name 为 null 但我们想要除 null 之外的所有这些 id

如果在输入参数为 null 的情况下选择了所有行,但排除具有 null 的值,那么如何做到这一点,否则它将使条件为 where name=null。

4

1 回答 1

0

对我来说,它看起来像

create or replace procedure p_proc 
  (par_user_id in number, par_name in number, cursor_c out sys_refcursor)
is
begin
  open cursor_c for
    select name, email, mobile, phone, address, department, grade, scale
    from employee 
    where (user_id in (select regexp_substr(par_user_id, '[^,]+', 1, level)
                       from dual
                       connect by level <= regexp_count(par_user_id, ',') + 1
                      )
           or par_user_id is null
          )
      and (name in (select regexp_substr(par_name, '[^,]+', 1, level)
                    from dual
                    connect by level <= regexp_count(par_name, ',') + 1
                   )
           or par_name is null
          );
end;          
于 2021-06-28T20:19:56.313 回答