0

我有一个简单的存储过程:

SELECT NVL(pnum,0)
      INTO emp_pnum 
      FROM employee_pnum 
     WHERE person_id = i_person_id
       AND pn_status_id = (SELECT pn_status_id FROM ems.pn_status WHERE pn_status_desc = 'Active');

SELECT NVL(pnum,0)
      INTO old_pnum 
      FROM employee_pnum_history
     WHERE person_id = i_person_id    
       AND created_date = (SELECT MAX(created_date) FROM ems.employee_pnum_history WHERE person_id = i_person_id);

OPEN x_pnum_history
   FOR SELECT emp_pnum,old_pnum
         FROM dual;

所以光标x_pnum_history被返回。我想要的只是将结果集读入一个类,如下所示

EmployeePNum empPnum = new EmployeePNum();
empPnum.setCurrent_pnum(rs.getString("emp_pnum"));
empPnum.setOld_pnum(rs.getString("old_pnum"));

它说

CallableStatementCallback; bad SQL grammar [{call EMS.EMS_PKG.GET_EMP_PNUM(?, ?, ?, ?)}];
nested exception is java.sql.SQLException: Invalid column name

我不确定这里到底出了什么问题......我们非常感谢任何帮助。

4

1 回答 1

0

当你这样做时:

OPEN x_pnum_history
   FOR SELECT emp_pnum,old_pnum
         FROM dual;

...您正在从 PL/SQL 块的范围中选择值,但您生成的列没有名称。您可以看到与此类似的内容(通过 SQL Developer 运行):

var rc refcursor

declare
  emp_pnum number;
begin
  emp_pnum := 1;
  open :rc for select emp_pnum as emp_pnum from dual;
end;
/

anonymous block completed

print rc

RC
---------------------------------------
:B1                                     
--------------------------------------- 
1                                       

请注意,列标题显示了一个通用绑定标记,:B1。但是,如果我明确地为这些列加上别名,那么它会执行:

declare
  emp_pnum number;
begin
  emp_pnum := 1;
  open :rc for select emp_pnum as emp_pnum from dual;
end;
/

anonymous block completed

print rc

RC
---------------------------------------
EMP_PNUM                                
--------------------------------------- 
1                                       

因此,在您的情况下,您需要更改程序以包含别名:

OPEN x_pnum_history
   FOR SELECT emp_pnum as emp_pnum, old_pnum as old_pnum
         FROM dual;

当然,这假设您的驱动程序和版本可以处理命名参数。不过,我认为您仍然可能需要在getString()通话中使用大写名称。

于 2013-10-03T15:31:39.140 回答