我需要一个存储过程,当在UI屏幕中使用一个值或任何值的组合进行搜索时,将提供结果。
用户界面屏幕:
Emp ID: _____________ FirstName:_______________ LastName:_______________
Salary:___________ Org:_______________ Location:__________ Country:__________
Search button Cancel button
当用户在这些字段中输入一个值或所有值或值的组合并单击“搜索”按钮时,我不确定如何获得结果。
数据来自Oracle数据库中名为Employee的表。
代码:
Procedure search_emp(p_id in NUMBER,
fname in VARCHAR2,
lname in VARCHAR2,
sal in NUMBER,
org in VARCHAR2,
location in VARCHAR2,
country in VARCHAR2
cursor1 OUT REF CURSOR)
BEGIN
OPEN cur FOR
select emp_id, fname,lname,sal,org,location,country from Employee;
--- I'm not sure how to get the results when the User enters either one value or all values or combination of values in those fields.
END;
请帮助。
答案 0 :(得分:2)
用于过程外观列名称的参数名称。我建议您不要这样做,因为您会得到意想不到的结果。例如,如果您将其设置为where fname = fname
(第一个fname
是列名,第二个则是参数),就好像您写了where 1 = 1
(忽略此null)例);在参数名称前加上p_
(或任何您认为合适的名称)。另外,请始终使用表别名(在我的示例中为e.
)。
然后,您的代码可能如下所示:
procedure search_emp (p_id in number,
p_fname in varchar2,
p_lname in varchar2,
p_sal in number,
p_org in varchar2,
p_location in varchar2,
p_country in varchar2,
cursor1 out ref cursor)
is
begin
open cursor1 for
select e.emp_id,
e.fname,
e.lname,
e.sal,
e.org,
e.location,
e.country
from employee e
where (upper(e.fname) like '%' || upper(p_fname) || '%' or p_fname is null)
and (upper(e.lname) like '%' || upper(p_lname) || '%' or p_lname is null)
and (e.sal = p_sal or p_sal is null)
and (upper(e.org) like '%' || upper(p_org) || '%' or p_org is null)
and (upper(e.location) like '%' || upper(p_location) || '%' or p_location is null)
and (upper(e.country) like '%' || upper(p_country) || '%' or p_country is null);
end;