插入新行,使用Oracle SQL中的合并,插入,更新语句更新行



当我尝试运行这个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匹配;然后更新数据,否则需要从视图的值将数据插入到表中。

非常感谢。

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 个逗号错误,它就像一个魅力。现在正在从视图中提取数据并与表匹配。 如果没有匹配项,它会将视图中的值插入到表中。

有关更新的 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;

相关内容

  • 没有找到相关文章

最新更新