I'm trying to use PostgreSQL IF sentence and I use MapSqlParametersSource
to pass parameter to the SQL query. Interesting thing happens that if I pass a parameter to IF condition subquery it won't accept (interpret it properly) it, but if I define value in subquery then it will give me the results. So what I mean is this:
This works
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("recurr", true);
String sql = "DO " +
"$do$ " +
"BEGIN " +
"IF (SELECT f.recurring_till FROM FINANCE_ENTITY f WHERE f.recurring = true) THEN SELECT amount, name FROM FINANCE_ENTITY; " +
"END IF; " +
"END " +
"$do$";
getNamedParameterJdbcTemplate().query(sql, params, BeanPropertyRowMapper.newInstance(FinanceEntity.class));
This return me results successfully.
This won't work
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("recurr", true);
String sql = "DO " +
"$do$ " +
"BEGIN " +
"IF (SELECT f.recurring_till FROM FINANCE_ENTITY f WHERE f.recurring = :recurr) THEN SELECT amount, name FROM FINANCE_ENTITY; " +
"END IF; " +
"END " +
"$do$";
getNamedParameterJdbcTemplate().query(sql, params, BeanPropertyRowMapper.newInstance(FinanceEntity.class));
This will give me always following error:
org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.
My question is why I can't pass a parameter to my subquery using MapSqlParameterSource
?
I use PostgreSQL 9.3.