1

I have the following query to create a partition, it's working with hardcoded values but is unable to replace parameters dynamically.

@Modifying
@Query(value = "CREATE TABLE :name PARTITION OF sample_table\n"
        + "FOR VALUES FROM (a) TO (b)", nativeQuery = true)
void createPartition(@Param("name") String name, @Param("a") long a, @Param("b") long b);

This statement I want to generate which is working fine on postgres console:

CREATE TABLE sample_table_p1 PARTITION OF sample_table
        FOR VALUES FROM ('1020000') TO ('2000000');

error:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"

Thanks in advance :)

Sachin Sharma
  • 41
  • 2
  • 10
  • 1
    You can't pass an identifier as a parameter. You need to dynamically create the SQL string with an explicit identifier –  Jul 21 '21 at 13:18
  • how can we pass the SQL query string to value? @a_horse_with_no_name – Sachin Sharma Jul 21 '21 at 13:30

0 Answers0