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))
Asked
Active
Viewed 1,580 times
-3

Chiranjhivi Ghimire
- 1,739
- 1
- 19
- 21
-
looks like you made an error in your sql, but seriously, this is way too vague to check. – Stultuske Jan 17 '19 at 07:02
-
Why does your query have both Ruby and HTML artifacts? – ruakh Jan 17 '19 at 07:06
-
Remove the `)` in `#{meetId})` – juergen d Jan 17 '19 at 07:07
-
ruakh please only focus on my sql condition check statement IF @source != 'MANUAL' is this correct or not? – Chiranjhivi Ghimire Jan 17 '19 at 07:11
-
Can you post full error message. Also post SQL code which passed to SQL driver without XML escaping and other stuff. – talex Jan 17 '19 at 07:14
-
When can you start an Sql statement with `WHERE` (`WHERE reg.is_waitlist = 0`...`)? – Joakim Danielson Jan 17 '19 at 07:19
-
To answer my own question, I now see that the query has been edited in a way that makes it incomplete and incorrect. – Joakim Danielson Jan 17 '19 at 07:25
-
2That is invalid standard SQL to begin with. Which DBMS product are you using? – Jan 17 '19 at 07:30
3 Answers
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).

Roman-Stop RU aggression in UA
- 14,905
- 3
- 48
- 53
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