使用%ROWTYPE创建pl / sql过程时出现问题

时间:2015-12-08 06:02:51

标签: oracle stored-procedures plsql rowtype

我在创建执行操作的过程时遇到问题。基本上我有两个表一个是INCOME表另一个是RECURRINGTRANSACTION。我打算在oracle 11g中创建一个每天执行一个程序的工作。程序将有一个开始日期,结束日期和发生次数,所以我的程序应该检查开始日期是今天的日期和发生次数!= 0然后它应该在我的第一个表中插入一行,即INCOME表。以下是我的表格说明和数据。enter image description here

enter image description here

enter image description here

create or replace procedure recurTransDaily(currTransID in number)
is

  type r_currentTransRow is record(
   RECTRANSACTIONID RECURRINGTRANSACTION.RECTRANSACTIONID%TYPE,
   AMOUNT RECURRINGTRANSACTION.AMOUNT%TYPE,
   DESCRIPTION RECURRINGTRANSACTION.DESCRIPTION%TYPE,
   ISEXPENSE RECURRINGTRANSACTION.ISEXPENSE%TYPE,
   ISINCOME RECURRINGTRANSACTION.ISINCOME%TYPE,
   NOOFOCCURENSES RECURRINGTRANSACTION.NOOFOCCURENSES%TYPE,
   TRANSACTIONDATE RECURRINGTRANSACTION.TRANSACTIONDATE%TYPE,
   TRANSSACTIONENDDATE RECURRINGTRANSACTION.TRANSSACTIONENDDATE%TYPE,
   CATEGORYID RECURRINGTRANSACTION.CATEGORYID%TYPE,
   PAYEEID RECURRINGTRANSACTION.PAYEEID%TYPE,
   RECURRINGTRANSACTIONTYPE     RECURRINGTRANSACTION.RECURRINGTRANSACTIONTYPE%TYPE,
   PERSONID RECURRINGTRANSACTION.PERSONID%TYPE);


BEGIN
   select *
   into r_currentTransRow
   from RECURRINGTRANSACTION where RECTRANSACTIONID=currTransID;

 if to_char(r_currentTransRow.TRANSACTIONDATE,'MM-dd- yyyy')=to_char(sysdate,'MM-dd-yyyy') then
 -- insert a record into income table
    insert into   income(AMOUNT,DESCRIPTION,TRANSACTIONDATE,CATEGORYID,PAYEEID,PERSONID) values
  (r_currentTransRow.AMOUNT,r_currentTransRow.DESCRIPTION,r_currentTransRow.TRANSACTIONDATE,
   r_currentTransRow.CATEGORYID,r_currentTransRow.PAYEEID,r_currentTransRow.PERSONID);

    -- update the present RECURRINGTRANSACTION row with TRANSACTIONDATE=sysdate+1 and NOOFOCCURENSES=NOOFOCCURENSES-1
   update RECURRINGTRANSACTION set  RECURRINGTRANSACTION.TRANSACTIONDATE=to_char(sysdate+1,'MM-dd-yyyy'),
        RECURRINGTRANSACTION.NOOFOCCURENSES=r_currentTransRow.NOOFOCCURENSES-1 
             WHERE   RECURRINGTRANSACTION.RECTRANSACTIONID=r_currentTransRow.RECTRANSACTIONID;
   commit;
 end if;
 END;

如果我执行上面的脚本,它会给我以下错误。我是PL / SQL的新手。有人可以解释错误的含义以及解决错误的方法 enter image description here

1 个答案:

答案 0 :(得分:2)

/ *您好,您可以尝试下面的代码段。由于我现在没有工作区,因此可能包含语法错误。如果这有帮助,请告诉我。 * /

CREATE OR REPLACE PROCEDURE recurTransDaily(
    currTransID IN NUMBER)
IS
type r_currentTransRowrcrd
IS
  record
  (
    RECTRANSACTIONID RECURRINGTRANSACTION.RECTRANSACTIONID%TYPE,
    AMOUNT RECURRINGTRANSACTION.AMOUNT%TYPE,
    DESCRIPTION RECURRINGTRANSACTION.DESCRIPTION%TYPE,
    ISEXPENSE RECURRINGTRANSACTION.ISEXPENSE%TYPE,
    ISINCOME RECURRINGTRANSACTION.ISINCOME%TYPE,
    NOOFOCCURENSES RECURRINGTRANSACTION.NOOFOCCURENSES%TYPE,
    TRANSACTIONDATE RECURRINGTRANSACTION.TRANSACTIONDATE%TYPE,
    TRANSSACTIONENDDATE RECURRINGTRANSACTION.TRANSSACTIONENDDATE%TYPE,
    CATEGORYID RECURRINGTRANSACTION.CATEGORYID%TYPE,
    PAYEEID RECURRINGTRANSACTION.PAYEEID%TYPE,
    RECURRINGTRANSACTIONTYPE RECURRINGTRANSACTION.RECURRINGTRANSACTIONTYPE%TYPE,
    PERSONID RECURRINGTRANSACTION.PERSONID%TYPE);
TYPE lv_tab
IS
  TABLE OF r_currentTransRowrcrd;
  r_currentTransRow lv_tab;
BEGIN

  EXECUTE IMMEDIATE 'ALTER SESSION set nls_timestamp_format = ''DD-MON-YY HH24:MI:SS.FF6''';
  EXECUTE IMMEDIATE 'ALTER SESSION set nls_date_format = ''DD-MON-YY''';

  SELECT * BULK COLLECT
  INTO r_currentTransRow
  FROM RECURRINGTRANSACTION
  WHERE RECTRANSACTIONID =currTransID;

  FOR I IN r_currentTransRow.FIRST..r_currentTransRow.LAST

  LOOP
    IF TO_CHAR(TO_TIMESTAMP(r_currentTransRow(i).TRANSACTIONDATE,'DD-MON-YY HH24:MI:SS.FF6'),'DD-MON-YY') = to_char(sysdate,'DD-MON-YY') THEN
      -- insert a record into income table

      INSERT
      INTO income
        (
          AMOUNT,
          DESCRIPTION,
          TRANSACTIONDATE,
          CATEGORYID,
          PAYEEID,
          PERSONID
        )
        VALUES
        (
          r_currentTransRow(i).AMOUNT,
          r_currentTransRow(i).DESCRIPTION,
          r_currentTransRow(i).TRANSACTIONDATE,
          r_currentTransRow(i).CATEGORYID,
          r_currentTransRow(i).PAYEEID,
          r_currentTransRow(i).PERSONID
        );
      -- update the present RECURRINGTRANSACTION row with TRANSACTIONDATE=sysdate+1 and NOOFOCCURENSES=NOOFOCCURENSES-1
      UPDATE RECURRINGTRANSACTION
      SET RECURRINGTRANSACTION.TRANSACTIONDATE   =SYSTIMESTAMP+1,
        RECURRINGTRANSACTION.NOOFOCCURENSES      =r_currentTransRow(i).NOOFOCCURENSES-1
      WHERE RECURRINGTRANSACTION.RECTRANSACTIONID=r_currentTransRow(i).RECTRANSACTIONID;

      COMMIT;
    END IF;
  END LOOP;
END;