带WHERE子句的FOR LOOP

时间:2019-09-12 16:37:45

标签: oracle plsql

这个问题已经提出过,但不是专门针对Oracle数据库提出的。

可以使用WHERE子句过滤FOR LOOP吗?例如,我想做类似的事情:

--LOG ERRORS
FOR err in c_errors WHERE trx_type='CYCLE_COUNT'
LOOP

    ...do some stuff

END LOOP; 

此代码给出错误:

PLS-00103: Encountered the symbol "WHERE" when expecting one of the following ...

有合适的语法吗?

这是光标定义。它获取周期计数和调整交易类型。但是在上面提到的日志错误部分中,我只想报告周期计数错误。当然,我可以使用单独的游标,但尝试使用一个游标来完成。

    CURSOR c_errors IS
        SELECT DISTINCT CC_ENTRY_INTERFACE_ID INTERFACE_ID
            ,ERROR_MESSAGE
            ,creation_date
            ,LAST_UPDATE_DATE
            ,'CYCLE_COUNT' TRX_TYPE
        FROM mtl_cc_interface_errors
        UNION
        SELECT DISTINCT TRANSACTION_INTERFACE_ID
            ,ERROR_EXPLANATION
            ,CREATION_DATE
            ,LAST_UPDATE_DATE
            ,'ADJUSTMENT'
        FROM mtl_transactions_interface 
        WHERE process_flag=3 
            AND error_code IS NOT NULL
        ORDER BY last_update_date DESC;

2 个答案:

答案 0 :(得分:2)

  

在c_errors中出错,在trx_type ='CYCLE_COUNT'

这在语义上是错误的。

作为选项之一,您可以做的是创建一个参数化的游标。 这是an example

情况1:参数为空。返回所有行

set serveroutput on;
declare
  cursor l_cursor ( param1 varchar2) is
    select *
      from (
            select level  as c1
                 , 'cycle_count' as trx_type
              from dual
            connect by level < 3
            union all
            select level  as c1
                 , 'adjustemnt' as trc_type
              from dual
            connect by level < 3
           ) q
    where param1 is null
       or trx_type = param1;
begin
  -- param1 = null. No filter applied
  for r in l_cursor(null) loop
    dbms_output.put_line('C1: ' || to_char(r.c1) || '; ' ||
                         'TRX_TYPE: ' || r.trx_type);
  end loop;
end;

结果:

CNT: 1; TRX_TYPE: cycle_count
CNT: 2; TRX_TYPE: cycle_count
CNT: 1; TRX_TYPE: adjustemnt
CNT: 2; TRX_TYPE: adjustemnt

案例1:按TRX_TYPE过滤

set serveroutput on;
declare
  cursor l_cursor ( param1 varchar2) is
    select *
      from (
            select level  as c1
                 , 'cycle_count' as trx_type
              from dual
            connect by level < 3
            union all
            select level  as c1
                 , 'adjustemnt' as trc_type
              from dual
            connect by level < 3
           ) q
    where param1 is null
       or trx_type = param1;
begin
  -- param1 = 'cycle_count'
  for r in l_cursor('cycle_count') loop
    dbms_output.put_line('C1: ' || to_char(r.c1) || '; ' ||
                         'TRX_TYPE: ' || r.trx_type);
  end loop;
end;

结果:

C1: 1; TRX_TYPE: cycle_count
C1: 2; TRX_TYPE: cycle_count

答案 1 :(得分:0)

尼克的回答是一个很好的解释。

这是如何使用它的另一个示例,方法是使用 FOR 语法声明内联光标:

BEGIN
  FOR r_product IN (
        SELECT 
            product_name, list_price 
        FROM 
            products
        WHERE list_price > 120
        ORDER BY list_price DESC
    )
  LOOP
     dbms_output.put_line( r_product.product_name ||
        ': $' || 
        r_product.list_price );
  END LOOP;
END;

来源: https://www.oracletutorial.com/plsql-tutorial/plsql-cursor-for-loop/