title: Incorrect syntax near ')'. SQL query is wrong while running on both DBeaver and on java mapper files?
body: <pre><code>  DECLARE @source as NVARCHAR(MAX) = (SELECT md.[source] FROM sports_service.meet.meet_detail md WHERE md.meet_id = #{meetId})
  
      WHERE reg.is_waitlist = 0 AND reg.cancelled_dt IS NULL 
            AND NOT EXISTS (
              SELECT 1 FROM sports_service.meet.meet_invitations i
              WHERE i.meet_id = pmm.meet_id AND i.sports_person_id = reg.sports_person_id) 
       IF @source != 'MANUAL'
            AND EXISTS (
              SELECT 1 
              FROM sports_service.meet.session session
                  INNER JOIN sports_service.meet.event event ON event.session_id = session.id 
                  WHERE pmm.meet_id = session.meet_id
                      AND sports_service.dbo.fnGetMeetAge(p.birth_dt, detail.age_up_date, detail.id_format) &lt;![CDATA[&gt;=]]&gt; event.low_age
                      AND sports_service.dbo.fnGetMeetAge(p.birth_dt, detail.age_up_date, detail.id_format) &lt;![CDATA[&lt;=]]&gt; event.high_age
                      AND (event.eligible_gender IS NULL OR event.eligible_gender = p.gender))
  </code></pre>