Re: Query question

From: AnthonyThomas (Anthony.Thomas_at_CommerceBank.com)
Date: 01/06/05


Date: Wed, 5 Jan 2005 19:54:31 -0600

The CASE statement is there to include or exclude that condition in the
query; however, it is a lousy implementation. You are obviously using
dynamic sql inside of a stored procedure. Other than a convenient place to
put it, dyanmical sql inside a proc reduces the effectiveness of using
stored procedure.

The '%' before and after the passed in parameter are wildcard characters
that allow any string as a substitute. So, any string plus parameter plus
any string becomes the search condition. You are gauranteed to do a table
scan or clustered index scan as that criteria could never be supported by an
index.

As a better solution, try something more like this:

SELECT Col1, Col2, ..., Coln
FROM Tab1 JOIN Tab2
    ON Tab1.Key1 = Tab2.Key1
    AND Tab1.Key1 = Tab2.Key2
    ...
    AND Tab1.Keyn = Tab2.Keyn
    ...
    ...
    JOIN Tabn
    ON ...

WHERE criterion1 AND criterion2 ... AND criterionN
    AND (@stringvar IS NULL
        OR TabX.ColX LIKE (@stringvar + '%')
        )

This is executed directly. There is no need for a variable nor the use of
the EXEC(@var) function. TabX.ColX can be indexed and used if it is highly
selectable. The query execution plan can be reused.

Hope this helps.

Sincerely,

Anthony Thomas

-- 
"Paul" <Paul@discussions.microsoft.com> wrote in message
news:B39C178F-3ABF-4EB2-99F1-3BEA896DC966@microsoft.com...
Hi I have a large stored procedure that performs several queries
conditionally on a database depending on values that are passed into the
procedure.  Anyhow I am using a string search and have set up
SELECT @stringvar='%' + @stringvar + '%'
then have
case when @stringvar IS NOT NULL then
'AND (table.field LIKE @stringvar)'
ELSE ''
It works fine just could not remember why I needed to have the
SELECT @stringvar='%' + @stringvar + '%' statement.
thanks,
-- 
Paul G
Software engineer.


Relevant Pages

  • RE: SQL stored procedure executing twice
    ... I wasn't aware that DLookupwould execute the "domain" more than once. ... caused the stored procedure to execute twice. ... Dim stDocName As String ... My pass-thru query properties ...
    (microsoft.public.access.modulesdaovba)
  • RE: ODBC query in VB code Need HELP
    ... I am trying to get a stored procedure to run on info I send it I do not need ... I played around and got the code below to work using a pass-through query, ... Dim strpass As String ...
    (microsoft.public.access.formscoding)
  • RE: MS Query "Connections"
    ... handle that with my connection string. ... for using the stored procedure route is that it is faster. ... If you truly want to pass in the query string, ...
    (microsoft.public.excel.programming)
  • Re: Database Engine Tuning Advisor suggestion to replace syntax.
    ... stored procedure, the user needs EXECUTE rights to the stored procedure. ... Query 1 requires the user to have SELECT rights on the Customers ... Also, FWIW, the dynamic SQL is only syntax checked at run time, not when the ...
    (microsoft.public.sqlserver.tools)
  • RE: MS Query "Connections"
    ... Yes - you can pass a query string through ADO. ... for using the stored procedure route is that it is faster. ... connection once and close it when you are done AND in your error handler. ...
    (microsoft.public.excel.programming)