Search code examples
sqlibatisdynamic-sql

dynamic query error in ibatis


I got an error with a dynamic query. This is the query that i have:

<dynamic>
    select * from Tbl_Member
    WHERE

    <isNotNull property="FirstName">
      FIRST_NAME = #lastName#
    </isNotNull>

    <isNotNull prepend="and" property="lastName">
      LAST_NAME = #lastName#
    </isNotNull>

    <isNotNull prepend="and" property="">

    </isNotNull>

     .
     .
     .
     .

</dynamic>

If the property FirstName is not null, the query works. But if the property is null and the other left property LastName is not null, I get a SQL error.

Because in this condition the SQL statement becomes as follow:

  select * from Tbl_Member WHERE AND LAST_NAME = ? ...

How can I remove the first-prepended AND if the first property becomes Null

P.S:

I also tried removeFirstPrepend="true"

<isNotNull prepend="and" property="lastName" removeFirstPrepend="true">
  LAST_NAME = #lastName#
</isNotNull>

But unfortunately, it doesn't work either.


Solution

  • try with

    <dynamic prepend="WHERE">
    

    select * from Tbl_Member
    <dynamic prepend="WHERE">
    
    <isNotNull prepend="and" property="FirstName">
      FIRST_NAME = #lastName#
    </isNotNull>
    
    <isNotNull prepend="and" property="lastName">
      LAST_NAME = #lastName#
    </isNotNull>
    
    <isNotNull prepend="and" property="">
    
    </isNotNull>
    
     .
     .
     .
     .
    
    </dynamic>
    

    http://ibatis.apache.org/docs/dotnet/datamapper/ch03s09.html