引起:org.postgresql.util.PSQLException:错误:“RETURNING”或其附近的语法错误

时间:2015-10-05 20:03:45

标签: spring postgresql stored-procedures jdbctemplate

当我尝试将用户详细信息插入表

时,这是错误堆栈
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "RETURNING"
  Position: 52
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1911)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:173)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:618)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:468)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:414)
    at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)
    at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)
    at org.springframework.jdbc.core.JdbcTemplate$3.doInPreparedStatement(JdbcTemplate.java:903)
    at org.springframework.jdbc.core.JdbcTemplate$3.doInPreparedStatement(JdbcTemplate.java:900)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:629)

我正在使用存储过程来进行所有数据库交互

CREATE OR REPLACE FUNCTION ezhire."insert_user"
(
  in_name character varying, 
  in_email character varying,
  in_image_url character varying, 
  in_provider character varying
) 
RETURNS integer AS $$
DECLARE
  out_inserted_id integer;
BEGIN

  WITH rows as (
    INSERT INTO ezhire.user(name, email, image_url, provider)
    VALUES(in_name, in_email, in_image_url, in_provider)
    RETURNING id
  )
  SELECT id INTO out_inserted_id FROM rows;

  RETURN out_inserted_id;

END;
$$ LANGUAGE plpgsql

我无法弄清楚出了什么问题......

3 个答案:

答案 0 :(得分:0)

更改WITH部分,如下所示

  WITH test as (
    INSERT INTO ezhire.user(name, email, image_url, provider)
    VALUES(in_name, in_email, in_image_url, in_provider)
    RETURNING id as recentid
  )
  SELECT recentid INTO out_inserted_id FROM test;

(或)使其没有像

这样的WITH子句
CREATE OR REPLACE FUNCTION ezhire."insert_user"
(
  in_name character varying, 
  in_email character varying,
  in_image_url character varying, 
  in_provider character varying
) 
RETURNS integer AS $$
DECLARE
  out_inserted_id integer;
BEGIN

    INSERT INTO ezhire.user(name, email, image_url, provider)
    VALUES(in_name, in_email, in_image_url, in_provider)
    RETURNING id INTO out_inserted_id;


  RETURN out_inserted_id;

END;
$$ LANGUAGE plpgsql

答案 1 :(得分:0)

你基本上有两个选择:

<强> PL / pgSQL的

您需要提供id值的目标:

CREATE OR REPLACE FUNCTION ezhire."insert_user"
(
  in_name character varying, 
  in_email character varying,
  in_image_url character varying, 
  in_provider character varying
) 
RETURNS integer AS $$
DECLARE
  out_inserted_id integer;
BEGIN
  INSERT INTO ezhire.user(name, email, image_url, provider)
  VALUES (in_name, in_email, in_image_url, in_provider)
  RETURNING id INTO out_inserted_id;

  RETURN out_inserted_id;
END;
$$ LANGUAGE plpgsql;

SQL函数

看到你只在函数中执行纯SQL,使用更快更简单的SQL语言函数:

CREATE OR REPLACE FUNCTION ezhire."insert_user"
(
  in_name character varying, 
  in_email character varying,
  in_image_url character varying, 
  in_provider character varying
) 
RETURNS integer AS $$
  INSERT INTO ezhire.user(name, email, image_url, provider)
  VALUES (in_name, in_email, in_image_url, in_provider)
  RETURNING id;
$$ LANGUAGE sql;

答案 2 :(得分:0)

问题出在我的春季代码中:

需要更改此代码

public Role createRole(final Role role) {
        PreparedStatementCreator psc = new PreparedStatementCreator() {

            @Override
            public PreparedStatement createPreparedStatement(Connection conn)
                    throws SQLException {
                PreparedStatement pstmt = conn
                        .prepareStatement(QUERIES.ROLE.INSERT_ROLE);
                int i = 1;
                pstmt.setString(i++, role.getName());
                pstmt.setString(i++, role.getAbbreviation());
                pstmt.setString(i++, role.getInfo());
                logger.debug(pstmt.toString());
                return pstmt;
            }
        };
        Integer roleId = jdbcTemplate.query(psc,
                new ResultSetExtractor<Integer>() {
                    @Override
                    public Integer extractData(ResultSet rs)
                            throws SQLException, DataAccessException {
                        if (rs.next()) {
                            return rs.getInt("insert_role");
                        }
                        return null;
                    }
                });
        role.setId(roleId);
        return role;
    }

对此:

SELECT *
FROM mytable
<foreach item="item" index="index" collection="myhashmap.entrySet()" open=" WHERE (" separator=" OR ("> table.attribute= #{item.key} 
    <foreach item="item2" index="index" collection="#{item.value}" separator=" ," open=" AND table.attribute IN (" close="))">
       #{item2.value} 
         </foreach> 
 </foreach>