0

Here is the query-

sql = "SELECT EMP_NAME, EMP_NUMBER, EMP_JOINING_DATE, :joiningDate as NAMED_PARAM_DATE from EMP_TABLE where EMP_JOINING_DATE>:joiningDate"

code:

NamedParameterJdbcTemplate jdbcTemplate;
.....
MapSqlParameterSource sqlParams = new MapSqlParameterSource();
sqlParams.addValue("joiningDate", System.getenv("JOIN_DATE"), Types.DATE);
results = this.jdbcTemplate.query(sql, sqlParams, new BeanPropertyRowMapper<Employee.class>(Employee.class))

This gives me below error

nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-418, SQLSTATE=42610, SQLERRMC=unresolved untyped expression, DRIVER=4.25.1301

Is it possible to do that? I can do it alternate way but just wanted to check from Query perspective as we can select any hard-coded value in SQL query using any DB client.

Note: Updated the query for more clarity, also this code works fine when I take out joiningDate from SELECT statement.

San4musa
  • 277
  • 2
  • 12
  • I don't think you can use named parameters as column name placeholders (as opposed, for example, to values in a `where` clause). But, regardless, in this case, why can't you just change `:joiningDate as EMP_FIRST_DATE` to `EMP_JOINING_DATE as EMP_FIRST_DATE`? – andrewJames Feb 17 '23 at 20:48
  • Actually I simplied the query just to give the example but I have a complex query where I'm using the named parameter in multiple places in WHERE clause, INNER/OUTER join to compare (greater/less than equal) with different column so no specific column I can use in place of the named parameter. – San4musa Feb 17 '23 at 22:07

0 Answers0