0

我有一个从 3 个不同的表返回数据的存储过程。每次我在我的代码中调用该过程时,我都希望它将任何新数据插入到不同的表中(我知道重复数据不是最佳实践,但在这种情况下需要它)。

这是我到目前为止所拥有的:

BEGIN

DECLARE
 v_cc COMM_CUSTOMER%ROWTYPE;

CURSOR c1 is 
SELECT d.customer_name, d.active, f.street, f.state, f.zip, t.amount_due, t.due_date
FROM tables t 
INNER JOIN othertable on ect, ect
WHERE t.due_date < sysdate
c_row c1%rowtype;

BEGIN
      OPEN c1;
      LOOP
        FETCH c1
          INTO c_row;
        EXIT WHEN c1%NOTFOUND;
        MERGE INTO COMM_CUSTOMER cc
        USING DUAL
on (cc.customer_name= v_cc.customer_name and 
    cc.active = v_cc.active and
    cc.street = v_cc.street and
    cc.amount_due = v_cc.amount_due)

 when not matched then
      insert values c_row; 

    COMMIT;
  END LOOP;
  CLOSE c1; 

这编译但什么也不做。

我已经使用 oracle 大约一个星期了,对游标不太了解,总体上对 sql 没有太多经验……有人可以告诉我我做错了什么/另一种方法吗?谢谢!

4

2 回答 2

0

尝试这样的事情:

merge into COMM_CUSTOMER cc 
using 
(
  SELECT d.customer_name, d.active, f.street, f.state, f.zip, t.amount_due, t.due_date
  FROM tables t 
  INNER JOIN othertable on ect, ect
  WHERE t.due_date < sysdate 
) input
on (cc.customer_name= input.customer_name and 
    cc.active = input.active and
    cc.street = input.street and
    cc.amount_due = input.amount_due)
when not matched then
      insert values cc.customer_name, cc.active, cc.street, cc.state, cc.zip, cc.amount_due, cc.due_date; 

您应该阅读有关合并子句的更多信息

于 2013-07-01T05:12:03.873 回答
0

尝试这个:

DECLARE
     v_cc COMM_CUSTOMER%ROWTYPE;
     CURSOR c1
     IS
          SELECT d.customer_name,
               d.active,
               f.street,
               f.state,
               f.zip,
               t.amount_due,
               t.due_date
          FROM tables t
          INNER JOIN othertable
          ON (ect          = ect1)
          WHERE t.due_date < sysdate;
     c_row c1%rowtype;
BEGIN
OPEN c1;
LOOP
     fetch c1 into c_row;
     exit when c1%notfound;

merge into comm_customer cc 
using dual 
ON (cc.customer_name= v_cc.customer_name AND cc.active = v_cc.active AND cc.street = v_cc.street AND cc.amount_due = v_cc.amount_due)
WHEN NOT matched THEN
     INSERT
          ( col1_name, col2_name
          )VALUES
          (c_row.customer_name, c_row.active
          );
     COMMIT;
END LOOP;
CLOSE c1; 
END;
于 2013-07-01T05:09:15.253 回答