Search code examples
oracle-databasehibernatenamed-query

How to send Column Name as parameter in Named Query


I'm writing a Named query to fetch data's based on yearly bases.

Query

SELECT * FROM table1
   WHERE ((year1 > 0) OR (year2> 0 OR (beyondYear2> 0)));

The query is working fine. From JSP I'll be selecting the year, so for that particular year the record should load. The above query loads data for all the year range. Is there any way I can pass Column Name as parameter to avoid writing the query for every year?

SELECT * FROM table1
 WHERE ( :yearRange > 0);

:yearRange : as parameter.


Solution

  • You can use the parameter as a switch:

    SELECT * FROM table1
      WHERE ((year1 > 0 AND :yearRange='year1') OR (year2> 0 AND :yearRange='year2' OR (beyondYear2> 0 AND :yearRange='beyondYear2')));