如何使用substr / regex函数验证过滤器并从Oracle PLSQL中的存储过程中获取结果?

问题描述 投票:0回答:1

[当用户执行搜索地点,县或邮政编码(Emp_Address列中所有3个字符时,用逗号和空格分隔)时,我需要编写程序来检索员工的详细信息,需要帮助。

Employee表:

Emp_Address column consists ( Place, County (zipcode))

EMP_ID    Emp_Address           
--------------------------------------
1         Leeds, NorthYork  (ls2ph) 
2         London, NorthHam (tl9yh)  
3         Cunniham, Norwalk (tc1f1)     
4         Excel, Shire (cp14)   
5         Bradford, Clarkson (cr123) 

步骤:

Procedure search_emp (pt_zipCode in  varchar2,
                      pt_address in  varchar2,                       
                      empCursor  out ref cursor)
as
begin
open empCursor for
    select e.emp_id,
           e.emp_address       
    from employee e
    where ( UPPER(REGEXP_SUBSTR(Emp_Address, '\((.+)\)', 1, 1, NULL, 1)) = UPPER (pt_zipCode) or pt_zipCode IS NULL ) -- this is working fine when searched for zipCode directly

--- I'm not sure how to get the results when the User enters (First '2' characters only for place /county (or) entire place (or) entire county) in 'pt_address' input parameter.

end;

预期输出:

a) Search for "No" in 'pt_address' parameter.  Below 3 records should display

EMP_ID     Emp_Address          
--------------------------------------
1          Leeds, NorthYork (ls2ph) 
2          London, NorthHam (tl9yh) 
3          Cunniham, Norwalk (tc1f1)


b) Search "NorthHam" :

EMP_ID    Emp_Address
--------------------------------------
2         London, NorthHam (tl9yh)  


c) Search "Excel"

EMP_ID    Emp_Address
--------------------------------------
4          Excel, Shire (cp14)

请帮助。

sql regex oracle filter procedure
1个回答
0
投票
还有一种更简单的选择:INSTR

SQL> create or replace procedure search_emp 2 (pt_zipcode in varchar2, 3 pt_address in varchar2, 4 empcursor out sys_refcursor) 5 is 6 begin 7 open empcursor for 8 select emp_id, emp_address 9 from test 10 where instr(upper(emp_address), upper(pt_zipcode)) > 0 11 or instr(upper(emp_address), upper(pt_address)) > 0; 12 end; 13 / Procedure created.

测试:

SQL> var l_rc refcursor SQL> SQL> exec search_emp(null, 'No', :l_rc); PL/SQL procedure successfully completed. SQL> print l_rc; EMP_ID EMP_ADDRESS ---------- -------------------------------------------------- 1 Leeds, NorthYork (ls2ph) 2 London, NorthHam (tl9yh) 3 Cunniham, Norwalk (tc1f1) SQL> exec search_emp(null, 'NorthHam', :l_rc); PL/SQL procedure successfully completed. SQL> print l_rc; EMP_ID EMP_ADDRESS ---------- -------------------------------------------------- 2 London, NorthHam (tl9yh) SQL> exec search_emp(null, 'Excel', :l_rc); PL/SQL procedure successfully completed. SQL> print l_rc; EMP_ID EMP_ADDRESS ---------- -------------------------------------------------- 4 Excel, Shire (cp14) SQL>

© www.soinside.com 2019 - 2024. All rights reserved.