Oracle插入或更新。如果没有记录则INSERT;否则更新

时间:2015-01-23 09:24:14

标签: sql oracle plsql

我可以让这个程序运行,

CREATE OR REPLACE PROCEDURE WELTESADMIN.SP_MST_ERC_UPD
(
    p_HEAD_MARK IN MST_ERC_UPD.HEAD_MARK%TYPE,
    p_PROJECT_NAME IN MST_ERC_UPD.PROJECT_NAME%TYPE,
    p_COLI_NUM IN MST_ERC_UPD.COLI_NUM%TYPE,
    p_ONSITE_UPD_QTY IN MST_ERC_UPD.ONSITE_UPD_QTY%TYPE,
    p_PREP_UPD_QTY IN MST_ERC_UPD.PREP_UPD_QTY%TYPE,
    p_ERECT_UPD_QTY IN MST_ERC_UPD.ERECT_UPD_QTY%TYPE,
    p_QC_UPD_QTY IN MST_ERC_UPD.QC_UPD_QTY%TYPE
)
IS
BEGIN
UPDATE MST_ERC_UPD 
    SET 
        ONSITE_UPD_QTY = p_ONSITE_UPD_QTY,
        PREP_UPD_QTY = p_PREP_UPD_QTY,
        ERECT_UPD_QTY = p_ERECT_UPD_QTY,
        QC_UPD_QTY = p_QC_UPD_QTY
    WHERE
        HEAD_MARK = p_HEAD_MARK AND
        PROJECT_NAME = p_PROJECT_NAME AND
        COLI_NUM = p_COLI_NUM;
COMMIT;
END SP_MST_ERC_UPD;

这只是更新。我想要实现的是,当一个包含3个变量HEAD_MARK,PROJECT_NAME,COLI_NUM的行恰好存在于表中时,它应该更新该值,否则它应该使用提供的值插入新行。如何添加IF,ELSE以便根据这两个条件执行操作?

非常感谢

1 个答案:

答案 0 :(得分:4)

Oracle支持merge声明。您可以使用merge来避免多个insertupdatedelete语句。

请参阅以下网址: http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_9016.htm#SQLRF01606