如何在游标循环中使用Trunc(mydate)

时间:2015-10-20 17:17:10

标签: plsql oracle-sqldeveloper plsqldeveloper

我是PL / SQL的新手,今天我正在研究游标并得到一个场景,我需要根据日期+金额+ cust_id + txn_tpye获取重复的交易类型。一旦我得到重复项,我必须使用另一个游标或使用选择列值(日期+金额+ cust_id + txn_tpye)作为where子句的正常循环。

在此之前我只是尝试打印它们,如果我得到一个值,当我试图打印mydate值得到错误。向你们请求帮助。

SET SERVEROUTPUT ON

declare 

CURSOR dup_check

IS 

SELECT cust_id,amount,trunc(mydate),transaction_type,COUNT(1)
 FROM table_X WHERE trunc(mydate)>='10-OCT-2015' 
GROUP BY cust_id,amount,trunc(mydate),transaction_type
HAVING COUNT(1)>1 ;

BEGIN

FOR UP_REC IN dup_check

LOOP 

DBMS_OUTPUT.put_line(UP_REC.cust_id||'  '||UP_REC.amount||UP_REC.trnasaction_type||**trunc(mydate))**;

END LOOP;

END;


**PLS-00302: component 'mydate' must be declared**

1 个答案:

答案 0 :(得分:0)

为trunc(mydate)字段添加别名,如下所示,并将UP_REC.mydate放入dbms_output

SET SERVEROUTPUT ON
declare
CURSOR dup_check
IS
SELECT cust_id,
       amount,
       trunc(mydate) mydate, /* add an alias here */
       transaction_type,
       COUNT(1) 
 FROM table_X WHERE trunc(mydate) >= '10-OCT-2015' 
 GROUP BY cust_id,amount,trunc(mydate),transaction_type HAVING COUNT(1)>1 ;

BEGIN

FOR UP_REC IN dup_check
LOOP
DBMS_OUTPUT.put_line(UP_REC.cust_id||' '||UP_REC.amount||UP_REC.trnasaction_type||UP_REC.mydate));
END LOOP;

END;