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

How do I validate the filters using substr function and get results from Stored procedure in Oracle PLSQL?

我创建了一个存储过程,它在 where 子句中有 2 个过滤器。我包含了一个过滤器并且能够获得 results.However,当我尝试包含第二个过滤器时,程序没有显示任何值。

用户从 UI 屏幕输入参数并单击搜索按钮。

UI 屏幕:

员工 ID:_____________

国家:__________

                 Search button        Cancel button

数据来自 Oracle 数据库中名为 Employee 的table。

员工Table:

Emp_id ---- 姓名----- Phone 号码------ 国家

1----约翰史密斯----US-765-234-4567---美国

2---- Sam Benigal---- AL-978-346-765--- 阿根廷

3----马克泰勒----AS-987-3987-857---澳大利亚

4---- 克莱尔弗纳--- CA-85-454-5454--- 加拿大

对于第二个过滤器,我需要使用 phone 数字,其中包含以国家/地区代码开头的数据('US-765-234-4567' 代表美国;'AR-978-346-765' 代表阿根廷;'AS-987-3987-857' for Australia country...等等)并匹配输入参数'pt_country'生成结果。 我在这种情况下使用了 Substring 函数,因为基于 phone 数字中的前两个字符,我需要得到结果。

另外,输入参数"pt_country"可以有值也可以为NULL。

代码:

   Procedure search_emp (  pt_id        in     number,
                        pt_name       in     varchar2,
                        pt_country    in     varchar2,
                        empCursor    out    ref cursor)

as

    v_countrycode           CHAR(2);
    v_count                 NUMBER;
begin

    SELECT count(*) INTO v_count FROM r_country where UPPER(country) = UPPER(pt_country); 
    IF v_count > 0 THEN        
    SELECT countrycode INTO v_countrycode FROM r_country where UPPER(country) = UPPER(pt_country);            
    ELSE NULL; 
    END IF;  -- here, am trying to get the country code from the table for the input parameter 'pt_country' that is passed.

    open empCursor for
    select e.emp_id,
    e.name,
    e.phone_number           
    from employee e
    where upper(e.emp_id)    = upper(pt_emp_id)
    and   (UPPER(substr(e.phone_number,1,2)) LIKE UPPER(v_countrycode) or pt_country IS NULL);

--- I'm not sure how to get the results when the User gives a value to the input parameter  'pt_country'.
END;

请帮忙

尝试下面的程序,需要将光标定义为 SYS_REFCURSOR 以获得输出

   create or replace Procedure search_emp (  pt_emp_id        in     number,
                        pt_name       in     varchar2,
                        pt_country    in     varchar2,
                        empCursor    out    SYS_REFCURSOR)

as

    v_countrycode           CHAR(2);
    v_count                 NUMBER;
begin

    SELECT count(*) INTO v_count FROM r_country where UPPER(country) = UPPER(pt_country); 
    IF v_count > 0 THEN        
    SELECT countrycode INTO v_countrycode FROM r_country where UPPER(country) = UPPER(pt_country);            
    ELSE NULL; 
    END IF;  -- here, am trying to get the country code from the table for the input parameter 'pt_country' that is passed.

    open empCursor for
    select e.emp_id,
    e.name,
    e.phone_number           
    from employee e
    where upper(e.emp_id)    = upper(pt_emp_id)
    and   (UPPER(substr(e.phone_number,1,2)) LIKE UPPER(v_countrycode) or pt_country IS NULL);

--- I'm not sure how to get the results when the User gives a value to the input parameter  'pt_country'.
END;

使用单个查询得到如下结果:

Select e.emp_id,
    e.name,
    e.phone_number           
from employee e
Join r_country c 
  On (e.phone_number like c.countrycode || '-%')
Where upper(e.emp_id)    = upper(pt_emp_id)
  And UPPER(c.country) = UPPER(COALESCE(pt_country, c.COUNTRY))

干杯!!