3

我正在对 DB2 进行简单的存储过程调用。当它调用存储过程时,它总是返回这个错误:

DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884, SQLERRMC=MEDIAN_RESULT_SET;PROCEDURE, DRIVER=3.66.46

========== Java 代码:

String JDBC_DRIVER = "com.ibm.db2.jcc.DB2Driver";
// STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);

// STEP 3: Open a connection
System.out.println("Connecting to database..."); 
conn = DriverManager.getConnection(DB_URL, USER, PASS);

// to execute the stored procedure.
System.out.println("CALL median_result_set(?)");
String sql = "CALL median_result_set(?)";
CallableStatement stmt1 = conn.prepareCall(sql);
stmt1.registerOutParameter(1, Types.DOUBLE);

stmt1.execute();
System.out.println("jdbcadapter->callproc after execute " + sql);
stmt1.close();

conn.close();

============== db2 clp 命令行有效:

c:SP>db2 call median_result_set(?)
 Value of output parameters 
 --------------------------
 Parameter Name  : MEDIANSALARY 
 Parameter Value : +7.68582000000000E+004

Result set 1
--------------
NAME      JOB   SALARY
--------- ----- ---------
Marenghi  Mgr    77506.75
O'Brien   Sales  78006.00

================ 存储过程定义:

CREATE PROCEDURE median_result_set
-- Declare medianSalary as OUT so it can be used to return values
(OUT medianSalary DOUBLE)
RESULT SETS 2
LANGUAGE SQL
BEGIN

   DECLARE v_numRecords INT DEFAULT 1;
   DECLARE v_counter INT DEFAULT 0;

   DECLARE c1 CURSOR FOR
      SELECT salary FROM staff
       ORDER BY CAST(salary AS DOUBLE);

  -- use WITH RETURN in DECLARE CURSOR to return a result set
  DECLARE c2 CURSOR WITH RETURN FOR
   SELECT name, job, salary
   FROM staff 
   WHERE CAST(salary AS DOUBLE) > medianSalary
   ORDER BY salary;

  -- use WITH RETURN in DECLARE CURSOR to return another result set
 DECLARE c3 CURSOR WITH RETURN FOR
    SELECT name, job, salary
    FROM staff
    WHERE CAST(salary AS DOUBLE) < medianSalary
    ORDER BY SALARY DESC;

 DECLARE CONTINUE HANDLER FOR NOT FOUND
   SET medianSalary = 6666; 

 -- initialize OUT parameter
 SET medianSalary = 0;

 SELECT COUNT(*) INTO v_numRecords FROM STAFF;

 OPEN c1;

   WHILE v_counter < (v_numRecords / 2 + 1) DO
     FETCH c1 INTO medianSalary;
     SET v_counter = v_counter + 1;
  END WHILE;
  CLOSE c1;

  -- return 1st result set, do not CLOSE cursor
  OPEN c2;

  -- return 2nd result set, do not CLOSE cursor
  OPEN c3;
END @
4

3 回答 3

10

基本上“SQLCODE=-440, SQLSTATE=42884”意味着找不到存储过程。

我看到一个很常见的原因是参数不匹配。

对于我的情况,我注意到在 java 代码中,我必须将模式名称放在存储过程名称的前面,例如,而不是 median_result_set(?),我应该使用 SCHEMANAME.median_result_set(?)

可以使用一些数据库管理工具找到此 SP 的 SCHEMANAME。

我不需要从命令行指定架构名称的原因:似乎当我在创建该 SP 时使用同一用户从 CLP 命令行调用 SP 时,不需要架构名称(因为在内部他们匹配)。当然,在命令行中指定模式总是正确的。我观察到 DB2 在内部使用用户名作为模式名。例如,如果“ADMINISTRATOR”创建了一个 SP,那么字符串“ADMINISTRATOR”就是它的模式名称,只要我在 Windows 上看到。

于 2013-08-22T15:19:35.073 回答
0

您找不到它,因为它实际上不存在...尝试改为调用 MEDIAN_RESULT_SET 或将过程创建为“median_result_set”...

以防万一有人遇到同样的问题;)

于 2017-02-08T07:39:28.423 回答
-3

错误 # 表示42884 No routine was found with the specified name and compatible arguments. 检查 url: http: //publib.boulder.ibm.com/infocenter/db2luw/v9r5/index.jsp ?topic=%2Fcom.ibm.db2.luw.messages.doc%2Fdoc%2Frdb2stt.html

并搜索 42884 错误代码。希望您能自己解决您的问题。

于 2013-08-21T21:47:09.410 回答