步骤一:建立临时表
create global temporary table t_temp
(COL varchar2(30))
    on commit preserve rows;执行该语句;步骤二:建procedure(在包体里面,定义就不写了),调用若满足function check_st_cursor_status,则给临时表赋值
  procedure init_temp_table as
    cursor st_cursor is
      select * from st_table;
  begin
    for cur in st_cursor loop
      if (check_st_cursor_status(cur.stid)) then
        insert into t_temp (COL) values (cur.stid);
        commit;
      end if;
    end loop;
  
  EXCEPTION
    when others THEN
      dbms_output.put_line(Sqlerrm);
      rollback;
  end init_temp_table;步骤三:
通过调用function,返回临时表里的数据集在执行到加红语句时,cur.stid确实有值,可是,读临时表,里面却没有数据???
请大家帮忙,如何在临时表里赋值。我的分比较少,不好意思。
谢谢:)

解决方案 »

  1.   

    在SQLPLUS中利用SET SERVEROUTPUT ON看存储过程的执行情况procedure init_temp_table as 
        cursor st_cursor is 
          select * from st_table; 
      begin 
        for cur in st_cursor loop 
          if (check_st_cursor_status(cur.stid)) then 
            insert into t_temp (COL) values (cur.stid); 
            dbms_output.put_line(cur.stid);
            commit; 
          end if; 
        end loop; 
      
      EXCEPTION 
        when others THEN 
          dbms_output.put_line(Sqlerrm); 
          rollback; 
      end init_temp_table; 执行上面的存储过程后,查询一个临时表SELECT * FROM t_temp;
      

  2.   

    先谢谢你。没有数据,
    我是想把符合条件的数据(而不是所有数据)放在一个表里,然后返回它们(通过cursor),
    难道使用临时表这样行不通??
    如果行不通,给个其他方法。谢谢
      

  3.   


    你应该是在存储过程执行完,在sql命令窗口 select * 吧???
    这样的话是不能查到的,因为你的session已经断掉了,临时表的数据就被清空了。
    可以这样:procedure init_temp_table 
    (
        p_cursor out cursor;
    )as 
        cursor st_cursor is 
          select * from st_table; 
      begin 
        for cur in st_cursor loop 
          if (check_st_cursor_status(cur.stid)) then 
            insert into t_temp (COL) values (cur.stid); 
            commit; 
          end if; 
        end loop; 
        
      open p_cursor for select * from t_temp ;  EXCEPTION 
        when others THEN 
          dbms_output.put_line(Sqlerrm); 
          rollback; 
      end init_temp_table; 
    这样在执行完存储过程后,打开返回的游标就能看到结果了。
      

  4.   

    谢谢lwmonster,解决了。
    以前没有搞懂session的具体含义。
    同样谢谢zcs_1 
    谢谢两位啊。