比较特定值后复制数据

copy data after comparing a specific value

我想制作一个带有输入参数的程序。更加具体, 我有 tables 像下面的 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,检查 select 来自 col4 的不同值 例如对于 col1=600 , col2=140/col3=2 和 col2=140/col3=3 Return 20 和 35

并在此 table 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;

你能帮我做吗? 谢谢

如果我没记错的话,您是在尝试从一个 table 获取值并尝试将它们插入另一个 table。那么,你可以这样。

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);