在Oracle SQL

时间:2016-01-22 15:23:59

标签: sql merge insert sql-update oracle12c

当我尝试运行此PL / SQL块时,无法引用我的别名。我收到了:

ORA-06550 Line xx, column x: 
PLS:00225: subprogram or cursor "V" reference is out of scope.  

这适用于我的'V'别名的每个实例。

我正在使用此链接作为创建逻辑的指南:http://www.oratable.com/oracle-merge-command-for-upsert/

如果CONTRACT_NO匹配,我想从View中提取数据;然后更新数据,否则需要从View的值中将数据插入表中。

非常感谢。

declare
begin
merge into APEX_EBS_EXTENSION.SIUPO_CONTRACTS_OBLIGATION_DOC DOC
using
(select CONTRACT_NO,
        VENDOR_ID, 
        FISCAL_YEAR     
        TRANSACTION_CODE,   
        NUM_ID,
        VENDOR_NAME,
        ADDRESS1,              
        ADDRESS2,
        CITY,   
        STATE,
        ZIP,    
        VENDOR_CODE,
        START_DATE,
        END_DATE,
        COMMENTS,
        MULTI_YEAR_START,
        MULTI_YEAR_END
        FROM APEX_EBS_EXTENSION.SIUPO_CONTRACTS_OBLIGATION_V
        WHERE CONTRACT_NO = :P6_SEARCH) V
        ON (V.CONTRACT_NO = DOC.CONTRACT_NO)
     when matched then update
     set
        V.VENDOR_ID = DOC.VENDOR_ID,
        V.FISCAL_YEAR = DOC.FISCAL_YEAR,
        V.TRANSACTION_CODE = DOC.TRANSACTION_CODE,
        V.NUM_ID = DOC.NUM_ID,
        V.VENDOR_NAME = DOC.VENDOR_NAME,
        V.ADDRESS1 = DOC.ADDRESS1,
        V.ADDRESS2 = DOC.ADDRESS2,
        V.CITY = DOC.CITY,
        V.STATE = DOC.STATE,
        V.ZIP = DOC.ZIP,
        V.VENDOR_CODE = DOC.VENDOR_CODE,
        V.START_DATE = DOC.START_DATE,
        V.END_DATE = DOC.END_DATE,
        V.COMMENTS = DOC.COMMENTS,
        V.MULTI_YEAR_START = DOC.MULTI_YEAR_START,
        V.MULTI_YEAR_END = DOC.MULTI_YEAR_END

when not matched then

insert (V.CONTRACT_NO, V.VENDOR_ID, V.FISCAL_YEAR, V.TRANSACTION_CODE, V.NUM_ID, V.VENDOR_NAME, V.ADDRESS1, V.ADDRESS2, V.CITY,
V.STATE, V.ZIP, V.VENDOR_CODE, V.START_DATE, V.END_DATE, V.COMMENTS, V.MULTI_YEAR_START, V.MULTI_YEAR_END)

values (DOC.CONTRACT_NO, DOC.VENDOR_ID, DOC.FISCAL_YEAR, DOC.TRANSACTION_CODE, DOC.NUM_ID, DOC.VENDOR_NAME, DOC.ADDRESS1, DOC.ADDRESS2, DOC.CITY,
DOC.STATE, DOC.ZIP, DOC.VENDOR_CODE, DOC.START_DATE, DOC.END_DATE, DOC.COMMENTS, DOC.MULTI_YEAR_START, DOC.MULTI_YEAR_END);

end;

1 个答案:

答案 0 :(得分:0)

发现问题是表没有数据,并试图在表中插入空白值。我只是翻转别名并纠正1个逗号错误,它就像一个魅力。现在,数据从View中拉出并与表格匹配。如果没有匹配项,它会将View中的值插入表中。

请参阅下文,了解更新的MERGE,UPDATE和INSERT语句:

declare
begin
merge into APEX_EBS_EXTENSION.SIUPO_CONTRACTS_OBLIGATION_DOC DOC
using
(select CONTRACT_NO,
        VENDOR_ID, 
        FISCAL_YEAR, --put in comma 
        TRANSACTION_CODE,   
        NUM_ID,
        VENDOR_NAME,
        ADDRESS1,              
        ADDRESS2,
        CITY,   
        STATE,
        ZIP,    
        VENDOR_CODE,
        START_DATE,
        END_DATE,
        COMMENTS,
        MULTI_YEAR_START,
        MULTI_YEAR_END
        FROM APEX_EBS_EXTENSION.SIUPO_CONTRACTS_OBLIGATION_V
        WHERE CONTRACT_NO = :P6_SEARCH) V
        ON (DOC.CONTRACT_NO = V.CONTRACT_NO)
        when matched then update
        set

        DOC.VENDOR_ID = V.VENDOR_ID,
        DOC.FISCAL_YEAR = V.FISCAL_YEAR,
        DOC.TRANSACTION_CODE = V.TRANSACTION_CODE,
        DOC.NUM_ID = V.NUM_ID,
        DOC.VENDOR_NAME = V.VENDOR_NAME,
        DOC.ADDRESS1 = V.ADDRESS1,
        DOC.ADDRESS2 = V.ADDRESS2,
        DOC.CITY = V.CITY,
        DOC.STATE = V.STATE,
        DOC.ZIP = V.ZIP,
        DOC.VENDOR_CODE = V.VENDOR_CODE,
        DOC.START_DATE = V.START_DATE,
        DOC.END_DATE = V.END_DATE,
        DOC.COMMENTS = V.COMMENTS,
        DOC.MULTI_YEAR_START = V.MULTI_YEAR_START,
        DOC.MULTI_YEAR_END = V.MULTI_YEAR_END



when not matched then
--flipped aliases for insert and values  from V. to .DOC and vice-versa
insert  (DOC.CONTRACT_NO, DOC.VENDOR_ID, DOC.FISCAL_YEAR, DOC.TRANSACTION_CODE, DOC.NUM_ID, DOC.VENDOR_NAME, DOC.ADDRESS1, DOC.ADDRESS2, DOC.CITY,
DOC.STATE, DOC.ZIP, DOC.VENDOR_CODE, DOC.START_DATE, DOC.END_DATE, DOC.COMMENTS, DOC.MULTI_YEAR_START, DOC.MULTI_YEAR_END)

values (V.CONTRACT_NO, V.VENDOR_ID, V.FISCAL_YEAR, V.TRANSACTION_CODE, V.NUM_ID, V.VENDOR_NAME, V.ADDRESS1, V.ADDRESS2, V.CITY,
V.STATE, V.ZIP, V.VENDOR_CODE, V.START_DATE, V.END_DATE, V.COMMENTS, V.MULTI_YEAR_START, V.MULTI_YEAR_END);

end;