declare aa pls_integer;
begin
aa := 7700;
Execute immediate 'select * from emp where empno < :p1' using aa;
end;
为什么select的结果不显示出来? 初学oracle, 谢谢

解决方案 »

  1.   

    execute immediate 里执行select与update、delete、insert into不一样,后者直接由结果,
    而执行select,必须根据查询返回的结果来处理,如果返回的是个简单值,可直接存到变量中:
    declare aa pls_integer;
    pcount number;
    begin
    aa := 7700;
    Execute immediate 'select count(*) into :a from emp where empno < :p1' using pcount,aa;
    end;
    如果是包含很多列的查询,那么要通过游标来进行处理:
    declare aa pls_integer;
    type t_cur is ref cursor;
    cur t_cur;
    r_row scott.emp%rowtype;
    begin
    aa := 7700;
    open cur for 'select *  from scott.emp where empno < :p1' using aa;
    loop
    fetch cur into r_row;
    exit when cur%NOTFOUND;
        dbms_output.put_line(r_row.empno||' '||r_row.ename);
    end loop;
    end;
      

  2.   

    select * from table where col=:p1不可以么?
      

  3.   

    使用带参光标。
    在定义一个光标时,需要设计好一个查询语句,该语句在光标定义后是不能修改的。所以光标的定义似乎缺乏灵活性和通用性。虽然在一个PL/SQL程序中可以定义若干个光标,但为了光标的适用性更强,PL/SQL支持带参数的光标。示例参考: