在存储过程中使用with子句

时间:2013-02-11 10:00:38

标签: oracle11g

  create or replace procedure proc
  as
begin
declare
time_to_stay number(3):=90
   with date_partitions as
  (select dbms_xmlgen.getxmltype('
    select p.table_owner, 
   p.table_name, 
   p.high_value
    from   all_part_key_columns k, 
   all_tab_cols c, 
   all_tab_partitions p
   where  k.owner = c.owner
       and    k.column_name = c.column_name
       and    k.name = c.table_name
       and    k.owner = p.table_owner
       and    k.name = p.table_name
       and    (c.data_type = ''DATE'' or 
       c.data_type like ''TIMESTAMP%'')') 
        as xml
        from   dual)

     SELECT  x.*
        FROM    date_partitions p, 
       xmltable('/ROWSET/ROW'
       passing p.xml
       columns table_owner varchar2(30) 
                 path '/ROW/TABLE_OWNER',
                table_name varchar2(30) 
                path '/ROW/TABLE_NAME',
              high_value varchar2(30) 
                path '/ROW/HIGH_VALUE'
           ) x
                  where   to_date(substr(x.high_value,
                  instr(high_value, '''')+2,
                  19),
             'yyyy-mm-dd hh24:mi:ss') <= sysdate-time_to_stay
          end;

当我执行代码时,我创建了程序。但不起作用。当我尝试编译该过程时,我收到以下错误。 错误(7,1):PLS-00103:当期待以下之一时遇到符号“WITH”:*&amp; = - +; &LT; /&gt; at in mod mod not rem&lt;&gt;或!=或〜=&gt; =&lt; =&lt;&gt;和/或喜欢像||之间的喜欢4喜欢multiset成员submultiset ...

如果我用开始部分执行此代码,它对我来说很好。

2 个答案:

答案 0 :(得分:0)

只需使用代码格式化程序,错误会直接跳到你的眼睛。在time_to_stay的行上,您省略了最后一个;

答案 1 :(得分:0)

你犯了一些错别字:

即:

您添加的

  1. DECLARE。存储过程不需要这样做。

  2. 在开始阻止之前需要time_to_stay

  3. time_to_stay上缺少半冒号。

  4. 你必须有一个for循环,因为这个sql可以返回2行以上。

  5. 这是一个只将信息输出到缓冲区的示例。

    SQL>   create or replace procedure proc
      2    as
      3  time_to_stay number(3):=90;
      4  begin
      5     for r_tab in (with date_partitions as
      6                   (select dbms_xmlgen.getxmltype('
      7                      select p.table_owner,
      8                     p.table_name,
      9                     p.high_value
     10                      from   all_part_key_columns k,
     11                     all_tab_cols c,
     12                     all_tab_partitions p
     13                     where  k.owner = c.owner
     14                         and    k.column_name = c.column_name
     15                         and    k.name = c.table_name
     16                         and    k.owner = p.table_owner
     17                         and    k.name = p.table_name
     18                         and    (c.data_type = ''DATE'' or
     19                         c.data_type like ''TIMESTAMP%'')') as xml
     20                      from dual)
     21                  select x.*
     22                    from date_partitions p,
     23                         xmltable('/ROWSET/ROW' passing p.xml columns table_owner varchar2(30) path
     24                                   '/ROW/TABLE_OWNER', table_name varchar2(30) path '/ROW/TABLE_NAME',
     25                                   high_value varchar2(30) path '/ROW/HIGH_VALUE') x
     26                   where to_date(substr(x.high_value, instr(high_value, '''') + 2, 19),
     27                                 'yyyy-mm-dd hh24:mi:ss') <= sysdate - time_to_stay)
     28    loop
     29      dbms_output.put_line(r_tab.table_owner||','||r_tab.table_name||','||r_tab.high_value);
     30    end loop;
     31  end;
     32  /
    
    Procedure created.