在Oracle存储过程中,Rownum可以用来控制返回的记录行数,对结果集进行分页等操作。以下是一些Rownum在Oracle存储过程中的应用示例:
CREATE OR REPLACE PROCEDURE getEmployeesByPage(
pageNum IN NUMBER,
pageSize IN NUMBER
)
IS
BEGIN
SELECT *
FROM employees
WHERE ROWNUM BETWEEN ((pageNum - 1) * pageSize) + 1 AND pageNum * pageSize;
END;
CREATE OR REPLACE PROCEDURE getTopEmployeesBySalary
IS
BEGIN
SELECT *
FROM employees
WHERE ROWNUM <= 10
ORDER BY salary DESC;
END;
CREATE OR REPLACE PROCEDURE getEmployeesByDepartment(
departmentId IN NUMBER
)
IS
BEGIN
SELECT *
FROM employees
WHERE department_id = departmentId
AND ROWNUM <= 10;
END;
这些示例展示了如何在Oracle存储过程中使用Rownum对结果集进行控制和筛选,以实现不同的查询需求。