如何在存储过程中编写from子句

时间:2014-09-26 06:34:22

标签: oracle stored-procedures plsql

需要在Procedure(在参数中使用)

中编写以下Query
 SELECT *
  FROM (SELECT a.ID,
               b.table_name,
               b.creation_date,
             RANK() OVER (PARTITION BY b.table_name ORDER BY b.creation_date desc) as date_ranking
     FROM  xxc_history a,
           xxc_history_details b
    WHERE  a.ID = b.ID
           AND name = P_NAME)
    WHERE   date_ranking = 1;

我需要在某个地方调用此程序。我正在使用 Oracle Database 11g企业版11.1.0.7.0版 - 64位生产

1 个答案:

答案 0 :(得分:0)

您可以在函数中编写此查询,并将结果集作为sysrefcursor

返回

下面给出了一个refcursor方法的例子:

 create or replace function f_example (p_name varchar2) return sys_refcursor is

     v_sysrefcursor sys_refcursor;

  Begin
    open v_sysrefcursor for ( SELECT *
                              FROM (SELECT a.ID,
                               b.table_name,
                               b.creation_date,
                               RANK() OVER (PARTITION BY b.table_name ORDER BY                b.creation_date desc) as date_ranking
                             FROM  xxc_history a,
                                   xxc_history_details b
                             WHERE  a.ID = b.ID
                             AND name = P_NAME)
                             WHERE   date_ranking = 1);

                             return v_sysrefcursor;
  end f_example ;

在另一个程序p_calling_proc中调用此函数:

  create or replace procedure p_calling_proc
   is

    v_sysrefcursor sys_refcursor;
     v_name varchar2(32);

  begin 
    v_name :='Vishad';

    v_sysrefcursor :=f_example(v_name);

 end p_calling_proc;

希望有所帮助

Vishad