0

I want to use a parameter called tags in a sql statement:

SELECT * FROM reply WHERE array[:tags] @> array[2293,2294];

I pass the parameter and execute the statement via:

MapSqlParameterSource params = new MapSqlParameterSource();
Integer[] a={2293,2294};
params.addValue("tags", Arrays.asList(a));
namedParameterJdbcTemplate.queryForObject(statement, params, String.class);

But it throws the error:

class org.springframework.dao.InvalidDataAccessApiUsageException No value supplied for the SQL parameter 'tags]': No value registered for key 'tags]'

If I change the statement to:

SELECT * FROM reply WHERE '{:tags}' @> array[2293,2294];

It throws the error:

class org.springframework.dao.DataIntegrityViolationException PreparedStatementCallback; SQL [SELECT * FROM reply WHERE '{:tags}' @> array[2293,2294]; ERROR: invalid input syntax for integer: ":tags" Position: 72; nested exception is org.postgresql.util.PSQLException: ERROR: invalid input syntax for integer: ":tags" Position: 72

If I change the statement to:

SELECT * FROM reply WHERE (:tags) @> array[2293,2294];

It throws the error:

class org.springframework.jdbc.BadSqlGrammarException PreparedStatementCallback; bad SQL grammar [SELECT * FROM reply WHERE (?, ?) @> array[2293,2294]; nested exception is org.postgresql.util.PSQLException: ERROR: operator does not exist: record @> integer[] Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 81

1

1 Answer 1

1

Separate brackets from :tags solves the problem:

SELECT * FROM reply WHERE array[ :tags ] @> array[2293,2294];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.