您还可以声明接受参数的静态游标,并在打开该游标时传递这些参数的值。在下面的示例中,我们创建了一个参数化游标,该游标将显示emp表中其薪水低于作为参数传递的指定值的所有员工的姓名和薪水。
DECLARE
my_record emp%ROWTYPE;
CURSOR c1 (max_wage NUMBER) IS
SELECT * FROM emp WHERE sal < max_wage;
BEGIN
OPEN c1(2000);
LOOP
FETCH c1 INTO my_record;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Name = ' || my_record.ename || ', salary = '
|| my_record.sal);
END LOOP;
CLOSE c1;
END;
例如,如果我们将值2000作为max_wage传递,那么将只显示薪水低于2000的所有员工的姓名和薪水。上述查询的结果如下:
Name = SMITH, salary = 800.00
Name = ALLEN, salary = 1600.00
Name = WARD, salary = 1250.00
Name = MARTIN, salary = 1250.00
Name = TURNER, salary = 1500.00
Name = ADAMS, salary = 1100.00
Name = JAMES, salary = 950.00
Name = MILLER, salary = 1300.00