背景
我正在开发一个调用 PLSQL 存储过程来检索和操作信息的 Web 应用程序。在一种这样的情况下,数据库有两个存储过程;一个选择给定参数集的记录总数,一个返回具有相同参数的实际记录,加上分页参数(最小和最大行数)。
EX(不是实际代码):
PROCEDURE get_records_count(
p_first_name IN record_table.first_name%TYPE,
p_last_name IN record_table.last_name%TYPE,
p_resultCursor OUT sys_refcursor
)
IS BEGIN
OPEN p_resultCursor FOR
SELECT count(*) from record_table
WHERE first_name LIKE (p_first_name || '%')
OR last_name LIKE (p_last_name || '%');
END;
PROCEDURE get_records(
p_first_name IN record_table.first_name%TYPE,
p_last_name IN record_table.last_name%TYPE,
p_min IN NUMBER,
p_max IN NUMBER,
p_resultCursor OUT sys_refcursor
)
IS BEGIN
OPEN p_resultCursor FOR
SELECT * from record_table
WHERE first_name LIKE (p_first_name || '%')
OR last_name LIKE (p_last_name || '%')
AND rownum >= p_min AND rownum <= p_max;
END;
是否有人认为这是一个好主意超出了我的立场范围。问题是,无论何时更改任何一个存储过程,结果都不匹配。短期解决方法是查看两个存储过程,确定哪个存储过程的选择标准是合适的,然后编辑另一个存储过程,使它们都匹配。
作为长期修复,我想更改 get_records_count 过程以调用 get_records 过程,然后返回将从生成的 sys_refcursor 返回的记录总数。
前任:
PROCEDURE get_records_count(
p_first_name IN record_table.first_name%TYPE,
p_last_name IN record_table.last_name%TYPE,
p_resultCursor OUT sys_refcursor
)
AS
v_recordsSelectedCursor sys_refcursor;
BEGIN
/*I understand that I will need to change some logic in get_records to
handle the case in which p_max is set to zero.
It should take this case to not apply an upper limit.*/
get_records(p_first_name,p_last_name,0,0,v_recordsSelectedCursor);
/*This is where I really have NO idea what I'm doing.
Hopefully, you can infer what I'm trying to do. */
OPEN p_resultCursor FOR
SELECT count(*) FROM v_recordsSelectedCursor;
END;
实际问题
如何选择将为 sys_refcursor 返回的记录数?结果数字需要在 sys_refcursor 中返回