SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Are the two queries the same?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

djj55
Constraint Violating Yak Guru

USA
337 Posts

Posted - 06/28/2013 :  15:33:44  Show Profile  Reply with Quote
Hello, I am trying to remove the "execute sp_executesql". So will these two queries get the same result? The FromTable has AcctNum, LName, FName, [State]. Notice the IF statement makes sure there will be at least one of the three criteria.
IF ISNULL(@LastName, '') <> '' or ISNULL(@FirstName, '') <> '' or ISNULL(@State, '') <> ''
BEGIN
-- Old way
    set @SQL = 'insert into SearchTable '
             + 'select top 100 AcctNum, ' + CAST(@Session AS VARCHAR(5)) 
             + ' from FromTable '
             + 'where 1 = 1 '
    if @LastName <> ''
        set @SQL = @SQL + ' and LName = ' + @Quote + @LastName + @Quote + ' '
    if @FirstName <> ''
        set @SQL = @SQL + ' and FName like ' + @Quote + '%' + @FirstName + '%' + @Quote + ' '
    if @State <> ''
        set @SQL = @SQL + ' and [State] = ' + @Quote + @State + @Quote + ' ';
    execute sp_executesql @SQL

-- Proposed way
    INSERT INTO SearchTable 
    SELECT TOP 100 AcctNum, @Session 
    FROM FromTable 
    WHERE 1 = 1 
        AND LNAME LIKE CASE WHEN ISNULL(@LastName , '') = '' THEN '%' ELSE @LastName END 
        AND FNAME LIKE CASE WHEN ISNULL(@FirstName, '') = '' THEN '%' ELSE '%' + @FirstName + '%' END 
        AND [STATE] LIKE CASE WHEN ISNULL(@State, '') = '' THEN '%' ELSE @State END; 
END --IF


djj

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1754 Posts

Posted - 06/28/2013 :  19:49:52  Show Profile  Reply with Quote
The results seem to be the same but I suspect that your performance will degrade using the newer approach. All three conditions will need to be checked and met. The only way to know is guess and test. If you had some knowledge of the inputs (e.g., they only ever pass in one parameter with values) you might be able to add some intelligence to the IF-THEN logic.

=================================================
The cure for anything is salt water -- sweat, tears, or the sea. -Isak Dinesen
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 06/29/2013 :  14:50:31  Show Profile  Reply with Quote
see this too

http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
337 Posts

Posted - 07/01/2013 :  10:34:49  Show Profile  Reply with Quote
Thank you both.
I had not considered the performance aspect of the new version.

Bustza Kool, my test data seems to work with same number of returned results, quick check of several rows looked good. But I worry and thought I would post here to see if I missed something.

visakh16, nice article. I think I will rewrite to use variables in the sql_executesql.

Thanks again,

djj
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 07/01/2013 :  10:37:58  Show Profile  Reply with Quote
you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.17 seconds. Powered By: Snitz Forums 2000