Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Are the two queries the same?

Author  Topic 

djj55
Constraint Violating Yak Guru

352 Posts

Posted - 2013-06-28 : 15:33:44
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
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2013-06-28 : 19:49:52
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

52326 Posts

Posted - 2013-06-29 : 14:50:31
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

352 Posts

Posted - 2013-07-01 : 10:34:49
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

52326 Posts

Posted - 2013-07-01 : 10:37:58
you're welcome

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

- Advertisement -