首先定义一个没有参数的游标,
示例如下:
declare
cursor temp_cursor is
select ename from emp where empno = '7782';
v_name emp.ename%type;
begin
open temp_cursor;
loop
fetch temp_cursor
into v_name;
exit when temp_cursor%notfound;
dbms_output.put_line(v_name);
end loop;
close temp_cursor;
end;
参数游标是带有参数的游标,在定义参数游标之后,当使用不同参数值多次打开游标时,可以产生不同的结果集,语法如下: cursor cursor_name(parameter_name datatype) is select_statement;
定义参数游标时,游标参数只能指定数据类型,而不能指定长度。
1:游标查询语句的查询条件带参数
示例如下:
declare
cursor temp_cursor(no number) is
select ename from emp where empno = no;
v_name emp.ename%type;
begin
open temp_cursor(7782);
loop
fetch temp_cursor
into v_name;
exit when temp_cursor%notfound;
dbms_output.put_line(v_name);
end loop;
close temp_cursor;
end;
这里用的是oracle的scott用户自带的emp来做例子,在上面的例子中,temp_cursor(no number)中no是游标的参数定义;open temp_cursor(7782);在打开游标的时候要根据定义的参数将实际的值传递进去;这里只是将参数应用到游标定义的查询语句中,参数的其它用法以后再做介绍。
附:
select * from emp where empno='7782'
查询结果:
EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
17782CLARKMANAGER78391981/6/92450.0010 对比上面游标运行的实际输出(dbms_output.put_line(v_name);):CLARK;