【问题标题】:How to convert this select statement into a stored procedure?如何将此选择语句转换为存储过程?
【发布时间】:2014-11-29 04:33:55
【问题描述】:

我有这个显示 pl/sql 脚本的文本(内容)的选择语句:

select text
from DBA_source
where type like '%PROCEDURE%' and name like '%JOB_HISTORY%'
order by line;

我想将其转换为存储过程,以便将 select 语句的“名称”条件作为输入,而不应预先定义为 show like '%job_history%.

如何做到这一点?

【问题讨论】:

  • 你可能需要一个存储函数,而不是一个存储过程。

标签: sql oracle stored-procedures oracle11g


【解决方案1】:

这是一个函数,而不是存储过程:

create type proc_tab is table of DBA_source.text%type;
/

create function select_procedure(p_name in film.title%type) return proc_tab
is
    l_proc_tab proc_tab := proc_tab();
    n integer := 0;
begin
    select text
    bulk collect into l_proc_tab
    from DBA_source
    where type like '%PROCEDURE%' and name like '%' || p_name || '%'
    order by line;
    return l_proc_tab;
end;
/

我还没有测试过这段代码,但它至少应该是一个好的开始。

【讨论】:

  • 这看起来会导致 RBAR (row-by-agonizing-row) 情况。有没有办法只返回查询生成的结果表?
  • 你可以做涉及游标的事情,但这不是 OP 要求的。
  • 为单个标量定义对象类型是非常没用的。只需执行create type proc_tab is table of DBA_source.text%type;,然后在函数内部SELECT text BULK COLLECT INTO l_proc_tab FROM DBA_source WHERE ...; return l_proc_tab;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 2020-04-03
  • 1970-01-01
相关资源
最近更新 更多