如何使用 substr/regex 函数验证过滤器并从 Oracle PLSQL 中的存储过程中获取结果?
How do I validate the filters using substr/regex function and get results from stored procedure in Oracle PLSQL?
我需要帮助编写一个程序来在用户执行搜索地点或县或邮政编码时检索员工的详细信息(所有 3 个都包含在 Emp_Address
列中,逗号和 space 分开)。
Employee
table:
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)
请帮忙。
有一个更简单的选项: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>
我需要帮助编写一个程序来在用户执行搜索地点或县或邮政编码时检索员工的详细信息(所有 3 个都包含在 Emp_Address
列中,逗号和 space 分开)。
Employee
table:
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)
请帮忙。
有一个更简单的选项: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>