【问题标题】:Inserting values to a table returned by a Stored Procedure in Oracle 11g在 Oracle 11g 中将值插入到存储过程返回的表中
【发布时间】:2013-08-19 01:51:22
【问题描述】:

我需要将存储过程返回的值插入到表中。 我的存储过程由多个选择语句和联合组成,如下所示:

create or replace
PROCEDURE          "my_str_proc" (
  emp_code     IN     VARCHAR2,
  hired_year   IN     VARCHAR2,
  q_bio        OUT SYS_REFCURSOR)
AS
BEGIN
  OPEN q_bio FOR
    select column list from table_1 where....and...
    UNION
    select column list from table_2 where....and...
    UNION
    select column list from table_3 where....and...
    UNION
    select column list from table_4 where....and...

 ORDER BY hired_year;

 RETURN;

 /* I plan to add the following statement to the existing proedure script:*/
 /* How can I get the return values after the procedure is done running 
    and inserted into a table ? */
    INSERT INTO Report_table (col1,col2,col3,col4,col5,col6,col7,col8,col9)
    VALUES (?????)

END;

我如何编写脚本,以便将程序最后返回的值插入到我创建的表中。 我找不到脚本示例,而且我是 Oracle 的新手。我们的 Oracle 是 11g

【问题讨论】:

  • 你的意思是你想从这个过程中插入,还是从任何调用这个过程的地方插入?
  • “返回值”是什么意思?返回值是什么?这是一个过程,而不是一个函数。没有返回值。请更具体。
  • 只做insert as select ...
  • 我从我的 ColdFusion 中调用了这个过程。它返回一个记录集,我使用 ColdFusion 将记录集显示为 PDF 报告,以便用户查看结果。它已经运行了好几年,但不知何故,这次记录集中有超过 5000 条记录,我的冷融合服务器抱怨并显示内存不足错误消息。当我在 SQL developer 中运行这个程序时,我在 6 秒内获得了记录集。所以问题显然出在 Coldfusion 服务器中(?)。因此,如果我可以将记录集插入到表中,那么我可以使用 ColdFusion 从该表中进行选择。
  • @OldProgrammer - q_bio OUT SYS_REFCURSOR

标签: oracle oracle11g


【解决方案1】:

假设您的临时表的结构将与引用游标返回的行相同(列数相同,对应列的类型相同),那么下面的代码应该可以工作:

declare
   my_row temp_table_name%rowtype;
   q_bio SYS_REFCURSOR;
begin
  my_str_proc( 'x', 'y', q_bio );
  LOOP
    FETCH q_bio INTO my_row;
    exit when q_bio%NOTFOUND;
    INSERT INTO temp_table_name VALUES my_row ;
  END LOOP;
  CLOSE q_bio;
end;
/

【讨论】:

  • 我没有使用临时表我将临时表名更改为永久表名。我把你的脚本放在 str 的末尾。 proc 并注释掉了 RETURN。当我运行代码时,我得到了一堆错误。我做错了吗?声明 my_row PernamentTableName%rowtype; q_bio SYS_REFCURSOR;开始 My_str_proc_name( 'x', 'y', q_bio );循环获取 q_bio INTO my_row; q_bio%NOTFOUND 时退出;插入 PernamentTableName 值 my_row ;结束循环;关闭 q_bio;结尾; - 返回;结束;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-16
相关资源
最近更新 更多