Does SQL Server optimize a LIKE query ('%%')?

I have a Stored Proc that searches through records.

The problem is that some search criteria that come from the user interface may be an empty string. Thus, when criteria are not specified, the LIKE operator becomes redundant.

How can I efficiently perform this search or sql server? Or does he optimize the LIKE query ('%%'), since that means there is nothing to compare?

The stored process is as follows:

ALTER PROC [FRA].[MCC_SEARCH]
@MCC_Code varchar(4),
@MCC_Desc nvarchar(50),
@Detail nvarchar(50)
AS
BEGIN             
       SELECT
             MCC_Code,
             MCC_Desc,
             CreateDate,
             CreatingUser

       FROM
              FRA.MCC (NOLOCK)
       WHERE
             MCC_Code LIKE ('%' + @MCC_Code + '%')
             AND MCC_Desc LIKE ('%' + @MCC_Desc + '%')
             AND Detail LIKE ('%' + @Detail + '%')
       ORDER BY MCC_Code

END
+3
source share
3 answers

The short answer is no. The long answer is absolutely not.

LIKE ('%%'), , ?

, .

WHERE column LIKE '%%'
WHERE column IS NOT NULL

IS NOT NULL , .

SQL Server:
, SQL Server MVP http://www.sommarskog.se/dyn-search.html ( )

, CONTAINS, SQL Server Fulltext.

+4

, . , .

, - -, : LIKE


:
LIKE 'Something%' -
LIKE '% Something' - . , , "REVERSE", .
LIKE '% Something%' - . LIKE.

+5

LIKE (%) , SQL Server ( , ) , .

, , ... , , ?

: :

select * from mytable

exec :

select * from mytable where description like '%'

SQL .

+3

Source: https://habr.com/ru/post/1793007/


All Articles