You should read Dynamic Search Conditions in T‑SQL by Erland Sommarskog.
If you use SQL Server 2008 or later, then use OPTION(RECOMPILE) and write the query like this:
SELECT *
FROM Customer
WHERE
(Name = @SearchValue OR @QueryAll='true')
OPTION (RECOMPILE);
I usually pass NULL for @SearchValue to indicate that this parameter should be ignored, rather than using separate parameter @QueryAll. In this convention the query becomes this:
SELECT *
FROM Customer
WHERE
(Name = @SearchValue OR @SearchValue IS NULL)
OPTION (RECOMPILE);
Edit
For details see the link above. In short, OPTION(RECOMPILE) instructs SQL Server to recompile execution plan of the query every time it is run and SQL Server will not cache the generated plan. Recompilation also means that values of any variables are effectively inlined into the query and optimizer knows them.
So, if @SearchValue is NULL, optimizer is smart enough to generate the plan as if the query was this:
SELECT *
FROM Customer
If @SearchValue has a non-NULL value 'abc', optimizer is smart enough to generate the plan as if the query was this:
SELECT *
FROM Customer
WHERE (Name = 'abc')
The obvious drawback of OPTION(RECOMPILE) is added overhead for recompilation (usually around few hundred milliseconds), which can be significant if you run the query very often.
Nameis nullable, the 3rd query will not return the rows whereNameis null. Also, read sql in the wild's catch-all queries article.