比较特定值后复制数据

时间:2015-07-18 11:50:02

标签: plsql procedure

我想用in参数创建一个过程。更具体, 我有像下面的TABLE1

这样的表
|COL1 | COL2 | COL3 | COL4 |  COL5 | COL6|
 ----------------------------------------
|600  |  140 |   2  |  10  |  1600 |  1  |
 ----------------------------------------
|600  |  140 |   2  |  20  |  1200 |  4  |
 ----------------------------------------
|600  |  140 |   2  |  15  |  1100 |  3  |
 ----------------------------------------
|600  |  140 |   2  |  35  |  1700 |  2  |
 ----------------------------------------
|600  |  140 |   3  |  10  |  1300 |  6  |
 ----------------------------------------
|600  |  140 |   3  |  15  |  1100 |  5  |
 ---------------------------------------- 

对于相同的COL1和col2 / col3,请检查从col4中选择不同的值 例如,对于col1 = 600,col2 = 140 / col3 = 2并且col2 = 140 / col3 = 3 返回20和35

并在此表中插入TABLE1行 600,140,​​3,20,1200,7(序号) 600,140,​​3,35,1700,8(序号)

但我不知道如何进行插入声明:(

    PROCEDURE COPY_COLUMNS  (    P_COL1         IN      A.COL1%TYPE,
                                     P_FROM_COL2    IN      B.COL2%TYPE,
                                     P_FROM_COL3    IN      B.COL3%TYPE,
                                     P_TO_COL2      IN      B.COL2%TYPE,
                                     P_TO_COL3      IN      B.COL3%TYPE,
                                     P_FLG1         IN      VARCHAR2,
                                     P_FLG2         IN      VARCHAR2,
                                     P_FLG3         IN      VARCHAR2,                                      
                                     P_FLG4         IN      VARCHAR2,
                                     P_FLG5         IN      VARCHAR2
                                ) IS


    CURSOR C1 IS SELECT *
                   FROM A
                  WHERE COL1 = P_COL1;

    CURSOR C2 IS SELECT *
                   FROM B
                  WHERE COL1  = P_COL1
                    AND COL2  = P_COL2
                    AND COL3  = P_COL3 ;

    BEGIN

    IF P_FLG1='N' OR P_FLG2='N' OR P_FLG3='N' OR  P_FLG4 ='N' OR P_FLG5 = 'N' THEN
        GOTO label; --do nothing
    END IF;

    IF P_FLG1 = 'Y' THEN
        select COL4
        FROM TABLE1
        WHERE COL1 = P_COL1 AND COL2 = P_FROM_COL2 AND COL3 = P_FROM_COL3
        MINUS
        select COL4
        FROM TABLE1
        WHERE COL1 = P_COL1 AND COL2 = P_TO_COL2 AND COL3 = P_TO_COL3
-- how to do insert ?

    END IF;

    IF P_FLG2 = 'Y' THEN
        select COL4
        FROM TABLE2
        WHERE COL1 = P_COL1 AND COL2 = P_FROM_COL2 AND COL3 = P_FROM_COL3
        MINUS
        select COL4
        FROM TABLE2
        WHERE COL1 = P_COL1 AND COL2 = P_TO_COL2 AND COL3 = P_TO_COL3
    END IF;
    -- ..........
    <<label>>
    END;
你可以帮帮我吗? 谢谢

1 个答案:

答案 0 :(得分:0)

如果我没错,你试图从一个表中获取值并尝试将它们插入另一个表中。那么,你可以这样。

Insert into YourTable(col1,col2,coln) 
              SELECT col1,col2,coln from anotherTable;

<强>更新

在阅读您最近的评论后,您似乎应该使用MERGE。在这种情况下,即使您不需要使用光标。

MERGE INTO destination D
   USING (SELECT col1, col2 FROM source
   WHERE [condition check]) S
   ON (D.id = S.id)
   WHEN NOT MATCHED THEN INSERT (D.col3, D.col4)
     VALUES (S.col3, S.col4);