-3
  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) <![CDATA[>=]]> event.low_age
                    AND sports_service.dbo.fnGetMeetAge(p.birth_dt, detail.age_up_date, detail.id_format) <![CDATA[<=]]> event.high_age
                    AND (event.eligible_gender IS NULL OR event.eligible_gender = p.gender))
Chiranjhivi Ghimire
  • 1,739
  • 1
  • 19
  • 21

3 Answers3

2

If you want to check some condition (AND EXISTS ... clause) only if some other condition holds (source is MANUAL) and you want to do that in one query instead of:

WHERE (...some condition ...) AND (.. some other condition ... )
IF @source != 'MANUAL'
    AND EXISTS (
        SELECT 1 ... )

which is not supported as it is not a correct sql syntax you can do:

WHERE (...some condition ...) AND (.. some other condition ... )
    AND (
       (
         (SELECT md.[source] 
          FROM sports_service.meet.meet_detail md 
          WHERE md.meet_id = #{meetId}
         ) = 'MANUAL'
       )
      OR EXISTS (
        SELECT 1 
        FROM ...
      )
    )

You need to check if the performance of the resulting query would be satisfactory.

It would be probably better to execute the query that gets the source first and then execute either query with the additional condition or without it depending on the source value. To do that you can generate the query in mybatis using dynamic SQL (including or excluding this additional condition).

1

As you can't use If inside expression, you can write your sql like this:

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)
    AND ( @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) >= event.low_age
    AND sports_service.dbo.fnGetMeetAge(p.birth_dt,
    detail.age_up_date,
    detail.id_format) <= event.high_age
    AND (event.eligible_gender IS NULL
    OR event.eligible_gender = p.gender)))
Santosh Karna
  • 119
  • 1
  • 3
  • 12
0

You can't use IF inside expression.

talex
  • 17,973
  • 3
  • 29
  • 66