Postgres错误:列定义列表仅允许返回“记录”的函数

时间:2013-03-05 09:11:35

标签: java sql postgresql plpgsql

public class DimensionStoneDaoHibernate extends HibernateDaoSupport implements DimensionStoneDao 
      {

    public Connection con = null;
    int i=0;

    public void setCon(Connection con) {
        this.con = getSession().connection(); 
    }

    public Connection getCon() {        

        setCon(con);        
        return con;
    } 
        public List getMineralByApplicationId(String appId) {
            List<String> mineral = new ArrayList<String>();

        PreparedStatement query =getCon().prepareStatement("select * from  multiple_mineral_report(?) as types(val1 varchar)",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);      

                query.setInt(1, Integer.parseInt(appId));               
                    ResultSet rs=query.executeQuery();

                    String value = new String();
                    i = 0;
                    while(rs.next()){               
                        value = rs.getString(1);
                        mineral.add(value);
                    }

        } 

}

在SQL函数multiple_mineral_report(?)之上返回字符串。执行上面的代码时,抛出以下异常:

  

org.postgresql.util.PSQLException:错误:列定义列表仅允许返回“记录”的函数

    CREATE OR REPLACE FUNCTION multiple_mineral_report(b integer)
  RETURNS character varying AS
$BODY$
declare
k varchar := '';    
sql_res record;
begin
 for sql_res in execute 'select  mineral_name from application_mineral as am inner join mineral as m on(am.mineral=m.mineral_id) where am.application_id = '||$1||'' loop
    k = k || sql_res.mineral_name || ', ';
 end loop;
 return k;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION multiple_mineral_report(integer) OWNER TO postgres;

1 个答案:

答案 0 :(得分:2)

您的错误在这里:

PreparedStatement query =getCon().prepareStatement(
"select * from  multiple_mineral_report(?) as types(val1 varchar)"
,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

如果已经明确定义了列定义列表,则不必(实际上不能)定义返回类型。您的功能定义为

RETURNS character varying

删除粗体部分,此特定错误应消失 但实际上,由于你的函数返回一个值,你想要调用你的函数:

SELECT multiple_mineral_report(?)