PL / SQL MERGE INTO编译错误

时间:2014-05-30 13:30:52

标签: plsql toad

我想使用PL / SQL MERGE在定义为

的单个表上执行UPSERT
DECLARE

v_create LONG;

v_drop LONG;

TABLE_DOES_NOT_EXIST exception;  
PRAGMA EXCEPTION_INIT(TABLE_DOES_NOT_EXIST, -942); -- ORA-00942

TABLE_NAME VARCHAR2(30) := 'B2BOWNER.SSC_Page_Map'; 

BEGIN

   BEGIN

       v_drop := 'DROP TABLE ' || TABLE_NAME; 

       EXECUTE IMMEDIATE v_drop;  

       EXCEPTION 

           WHEN TABLE_DOES_NOT_EXIST THEN NULL;

   END;

v_create := 'CREATE TABLE ' ||  TABLE_NAME || ' (
                PAGE_ID_NBR   NUMERIC(10)   NOT NULL Check(Page_ID_NBR > 0),
                PAGE_TYPE     VARCHAR2(50)  NOT NULL, 
                PAGE_DCPN     VARCHAR2(100) NOT NULL,
                PRIMARY KEY(Page_ID_NBR, Page_Type))';

EXECUTE IMMEDIATE v_create; 

COMMIT WORK;

COMMIT COMMENT 'Create Table'; 

EXCEPTION

    WHEN OTHERS THEN

        IF SQLCODE = -955 THEN

            NULL; -- suppresses ORA-00955 exception
        ELSE

            RAISE;
        END IF;

END;

使用以下PL / SQL MERGE尝试如下

CREATE OR REPLACE FUNCTION B2BOWNER.F_SSC_Page_Map_Update(
                            p_page_id   IN B2BOWNER.SSC_Page_Map.Page_ID_NBR%TYPE, 
                            p_page_type IN B2BOWNER.SSC_Page_Map.Page_Type%TYPE, 
                            p_page_dcpn IN B2BOWNER.SSC_Page_Map.Page_Dcpn%TYPE)

RETURN INTEGER

IS

   TABLE_DOES_NOT_EXIST exception;  
   PRAGMA EXCEPTION_INIT(TABLE_DOES_NOT_EXIST, -942); -- ORA-00942

BEGIN

  MERGE INTO SSC_Page_Map d
  USING (SELECT PAGE_ID_NBR, PAGE_TYPE from dual)
  ON(d.PAGE_ID_NBR = p_page_id AND d.PAGE_TYPE = p_page_type)
  WHEN MATCHED THEN UPDATE SET d.PAGE_ID_NBR= p_page_id, d.PAGE_TYPE=p_page_type
  WHEN NOT MATCHED THEN INSERT (PAGE_ID_NBR, PAGE_TYPE, PAGE_DCPN) VALUES (p_page_id,    p_page_type, p_page_dcpn); 

   COMMIT WORK; 

   COMMIT COMMENT 'Modifying Type and Description Only';

   RETURN 0;

   EXCEPTION

    WHEN TABLE_DOES_NOT_EXIST THEN

        RETURN -1;

    WHEN DUP_VAL_ON_INDEX THEN

        RETURN -2;

    WHEN INVALID_NUMBER THEN

        RETURN -3;

    WHEN OTHERS THEN

        RETURN -4;

END F_SSC_Page_Map_Update;

SHOW ERRORS PROCEDURE B2BOWNER.F_SSC_Page_Map_Update;

GRANT EXECUTE ON B2BOWNER.F_SSC_Page_Map_Update TO B2B_USER_DBROLE

我正在

Warning: compiled but with compilation errors
No errors.
Grant complete.

详细说明

[Warning] ORA-24344: success with compilation error
16/31   PL/SQL: ORA-00904: "PAGE_TYPE": invalid identifier
15/4    PL/SQL: SQL Statement ignored
(2: 0): Warning: compiled but with compilation errors

如何编写MERGE以便我可以编译PL / SQL函数而不会出错?

1 个答案:

答案 0 :(得分:0)

在我看来,MERGE语句应该是:

MERGE INTO SSC_PAGE_MAP d
  USING (SELECT p_page_id AS PAGE_ID_NBR, p_page_type AS PAGE_TYPE from dual)
    ON(d.PAGE_ID_NBR = p_page_id AND
       d.PAGE_TYPE = p_page_type)
  WHEN MATCHED THEN
    UPDATE SET d.PAGE_DCPN = p_page_dcpn
  WHEN NOT MATCHED THEN
    INSERT
      (PAGE_ID_NBR, PAGE_TYPE,   PAGE_DCPN)
    VALUES
      (p_page_id,   p_page_type, p_page_dcpn);

分享并享受。