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