Один из способов сделать это - разделить запрос на несколько запросов. Я не говорю, что это дает лучшую производительность, но:
select * from accounts
where mobile = @mobile
union
select * from accounts
where like @mobile +'%'
and not exists (select 1 from accounts where mobile = @mobile)
union
select * from accounts
where mobile like '%'[email protected]
and not exists (select 1 from accounts where like @mobile +'%')
union
select * from accounts
where mobile like '%'[email protected] +'%'
and not exists (select 1 from accounts where like '%'[email protected])
Что-то другое, что вы можете сделать, что более «programatic» является использование @@ROWCOUNT
, что должно дать более высокую производительность, так как он имитирует короткое замыкание.
select * from accounts
where mobile = @mobile
if @@rowcount = 0
select * from accounts
where like @mobile +'%'
if @@rowcount = 0
select * from accounts
where mobile like '%'[email protected]
if @@rowcount = 0
select * from accounts
where mobile like '%'[email protected] +'%'
SQL Server не поддерживает оценки BOOL «короткого замыкания», и нет никакой возможности, чтобы «включить его» либо .. .. –
@marc_s Вот почему я спрашиваю, как мы можем достичь * эквивалента * короткого замыкания, где статья –