1. 程式人生 > >DB2 不指定Schema調用存儲過程失敗 SQLCODE=-440, SQLSTATE=42884

DB2 不指定Schema調用存儲過程失敗 SQLCODE=-440, SQLSTATE=42884

DB2 不指定Schema調用存儲過程失敗 SQLSTATE=42884 Schema和訪問DB的用戶名不一致

■ 現象
DB2,Schema和訪問DB的用戶名不一致。
調用存儲過程時必須指定Schema,否則Error。

技術分享圖片

■ 錯誤代碼
存儲過程

        SET SCHEMA = "QUOTATION";
        SET CURRENT PATH = "SYSIBM","SYSFUN","SYSPROC","SYSIBMADM","DB2INST1";
        CREATE PROCEDURE COPY_QUOTATION_TABLES (。。。。。。)
        BEGIN
                。。。。。。
        END;

        GRANT EXECUTE ON PROCEDURE "QUOTATION"."COPY_QUOTATION_TABLES"( VARCHAR(36), VARCHAR(36), INTEGER, TIMESTAMP, INTEGER, INTEGER ) TO USER "DB2INST1" WITH GRANT OPTION;

Mapper.xml

        <update id="copyQuotationTables" statementType="CALLABLE" parameterType="map">
                CALL COPY_QUOTATION_TABLES (
                    #{quotationId, jdbcType=VARCHAR, mode=IN}
                    , #{newQuotationId, jdbcType=VARCHAR, mode=IN}
                    , #{userId, jdbcType=INTEGER, mode=IN}
                    , #{currentTime, jdbcType=TIMESTAMP, mode=IN}
                    , #{reasonTypeFlag, jdbcType=INTEGER, mode=IN}
                    , #{copyReason, jdbcType=INTEGER, mode=IN} )
        </update>

Mapper.java

        /**
         * Copy Quotation Tables 
         *
         * @param param the parameter
         */
        void copyQuotationTables(Map<String, Object> param);

Service.java

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("quotationId", quotationId);
        paramMap.put("newQuotationId", newQuotationId);
        paramMap.put("userId", userId);
        paramMap.put("currentTime", currentTime);
        paramMap.put("reasonTypeFlag", 1);
        paramMap.put("copyReason", 1);
        Mapper.copyQuotationTables(paramMap);

Tomcat context.xml

    <Resource name="jdbc/testDS" auth="Container" type="javax.sql.DataSource"
              driverClassName="com.ibm.db2.jcc.DB2Driver"
              url="jdbc:db2://127.0.0.1:50000/test:currentSchema=QUOTATION;"
              username="db2inst1" password="db2inst1" maxActive="100"  maxIdle="30"/>

■ 正確代碼
Tomcat context.xml中追加currentFunctionPath。

    <Resource name="jdbc/testDS" auth="Container" type="javax.sql.DataSource"
              driverClassName="com.ibm.db2.jcc.DB2Driver"
              url="jdbc:db2://127.0.0.1:50000/test:currentSchema=QUOTATION;currentFunctionPath=QUOTATION;"
              username="db2inst1" password="db2inst1" maxActive="100"  maxIdle="30"/>

如果想在DB工具(如:Toda)中調用存儲過程,但不指定Schema,
如:CALL COPY_QUOTATION_TABLES(。。。。。。)
需要在創建存儲過程時,指定Current Path

SET CURRENT PATH = "SYSIBM","SYSFUN","SYSPROC","SYSIBMADM","DB2INST1","QUOTATION";

■ 原因
DB2存儲過程(和函數)不受CURRENT SCHEMA控制,而是由CURRENT PATH控制。

DB2 不指定Schema調用存儲過程失敗 SQLCODE=-440, SQLSTATE=42884