Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Stored Proc with optional parameters

By well0549,

Sometimes people want a Stored Proc with optional parameters. A lot of times people will use a coalesce for this. The problem with optional paremeters is that the query plan will never be an exact mach for that specific query.

( You never know what paremeters are filled..... )

To solve this problem look at the following script. It will generate a query with the parameters filled that you have filled.

It is much faster than a coalesce variant. And the more parameters you add, the better this one gets.......

Some Advantages :
1. Very Easy to maintain
2. ALWAYS a good query plan
3. Add as much optional parameters as you want,
the resulting query will always be an exact mach of what YOU want.....
4. Beats a COALESCE by FAR..... ( 10 to 30 times faster )

Here is the example......

Total article views: 406 | Views in the last 30 days: 2
Related Articles

Coalesce Query

Coalesce Query


Coalesce on inner queries

Coalesce on inner queries


stored procedure with optional parameters

creating stored procedure with optional parameters


Cached Query Plan ...

cached query plan, WITH RECOMPILE, optional parameters

sqlinsider scripts    

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones