在两个不同的sql表中添加外部作用域的值

时间:2014-01-10 07:38:42

标签: sql oracle plsql oracle-sqldeveloper

在下面的程序中,我分别更新并插入docmeta和documenthistory表。 我已经使用代码计算了docmeta update中'paymentterm'的值,如下所示。现在我想在文档历史表中插入相同的payterm值(在下面以粗体和斜体突出显示)。但由于docmeta中的paymenterm范围有限,即使我写完整的报酬,我也无法在文档历史表中使用此值选择语句即SELECT paymentterm         来自temp_pay_term         WHERE pid = d.xProject_id         AND rownum< = 1

如何使下面的代码可行 .................................................. .................

CREATE OR REPLACE
PROCEDURE PAYTERMUPDATE
IS
  RecordCount INT;
  varDID      NUMBER(38);
BEGIN
  RecordCount :=0;
  SELECT COUNT(1)
  INTO RecordCount
  FROM temp_pay_term;
  IF RecordCount > 0 THEN
    FOR X       IN
    (SELECT DISTINCT r.ddocName
    FROM temp_Pay_Term ptq,
      docMeta d,
      revisions r
    WHERE TO_CHAR(ptq.pid) = d.xproject_id
    AND r.did              = d.did
    )
    LOOP
      SELECT MAX(did)
      INTO varDid
      FROM revisions r
      WHERE r.ddocname = X.ddocName
      GROUP BY r.ddocname;
      UPDATE docmeta d
      SET paymentterm =
        (SELECT paymentterm
        FROM temp_pay_term
        WHERE pid   = d.xProject_id
        AND rownum <= 1
        )
      WHERE d.did = vardid;
      INSERT
      INTO DOCUMENTHISTORY
        (
          dactionmillis,
          dactiondate,
          did,
          drevclassid,
          duser,
          ddocname,
          daction,
          dsecuritygroup,
          ***paymentterm***
        )
      SELECT to_number(TO_CHAR(systimestamp,'FF')) AS dactionmillis,
        TRUNC(systimestamp,'dd')                   AS dactiondate,
        did,
        drevclassid,
        'sysadmin' AS duser,
        ddocname,
        'Update' AS daction,
        dsecuritygroup,
        ***PAYMENTTERM***
      FROM revisions
      WHERE did = vardid;
    END LOOP;
    COMMIT;
  END IF;
END PAYTERMUPDATE; -- PL SQL code

1 个答案:

答案 0 :(得分:0)

如果我说得对,你可以使用returning into

返回 paymentterm
  declare
    ...
    varpayment number;
  begin
    ...

    update docmeta d
    set paymentterm =
        (select paymentterm
         from temp_pay_term
         where pid = d.xproject_id
           and rownum <= 1)
    where d.did = vardid
    returning paymentterm into varpayment;

    /* and then use varpayment */
    ... 
  end;

有关详细信息,请参阅returning into documentation