SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to Design, Build and Test a Dynamic Search Stored Procedure


How to Design, Build and Test a Dynamic Search Stored Procedure

Author
Message
dwain.c
dwain.c
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44517 Visits: 6431
Comments posted to this topic are about the item How to Design, Build and Test a Dynamic Search Stored Procedure


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
davoscollective
davoscollective
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3623 Visits: 1008
Great article, thanks for sharing. I've used similar constructs for flexible SSRS reports. I learned a few nice tricks, I particularly like the @debug and the line numbers, very nice.

Based on Celko's war on dynamic SQL, I wonder if you'll get a response here too. Surely that would be a mark of success Wink
dwain.c
dwain.c
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44517 Visits: 6431
davoscollective (11/5/2013)
Great article, thanks for sharing. I've used similar constructs for flexible SSRS reports. I learned a few nice tricks, I particularly like the @debug and the line numbers, very nice.

Based on Celko's war on dynamic SQL, I wonder if you'll get a response here too. Surely that would be a mark of success Wink


And thank you sir for taking the time to read the article and I'm happy to hear you learned a couple of tricks. I also have to apologize for the SQL formatting issues. I've got an email in to the editor to correct that.

If Joe Celko were to chime in with his thoughts on dynamic SQL, I'd consider it a badge of honor. Dynamic SQL certainly has its place, and this is most certainly one of them. There are cases where it can be overused and/or abused, so a healthy and productive discussion of that (here if necessary) would be most welcome.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)SSC Guru (129K reputation)

Group: General Forum Members
Points: 129198 Visits: 22584
Dwain does it again - nice article sir!


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
IdRatherCodeIt
IdRatherCodeIt
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 55
Nice article,
For truly dynamic behavior, do you think moving that responsibility to coding might reduce verbosity, error prone string concatenation, and allow more flexible dynamic searches?
I'm talking about solution s like dynamic linq in c#.
Or using predicates with linq.
That type of solution would be testable using formal unit tests
Scott Abrants
Scott Abrants
Right there with Babe
Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)

Group: General Forum Members
Points: 745 Visits: 406
A very nice and complete article - thank you for your efforts in creating this.
I like the Excel idea to manage some test cases and data, interesting approach.
Scott
odeddror
odeddror
Mr or Mrs. 500
Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)

Group: General Forum Members
Points: 521 Visits: 441
Hi there,

How do you fix the null value?

Execute dbo.Shipment_Tracking @custID = null
This will give all records

Thanks,
Oded Dror



Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (511K reputation)SSC Guru (511K reputation)SSC Guru (511K reputation)SSC Guru (511K reputation)SSC Guru (511K reputation)SSC Guru (511K reputation)SSC Guru (511K reputation)SSC Guru (511K reputation)

Group: General Forum Members
Points: 511234 Visits: 44296
Phil Parkin (11/5/2013)
Dwain does it again - nice article sir!


Ditto that! Nicely done, Dwain. Great explanations and revelation of code.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
sqldriver
sqldriver
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5196 Visits: 2578
Awesome article. I particularly like using NULLIF to check the search parameters.

Also a reminder that I wish the display more than 8k characters SQL Spackle were default server behavior. Thanks Dwain.
dwain.c
dwain.c
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44517 Visits: 6431
Phil, Scott, Jeff and Erik,

Thanks for stopping in to read the article and offer your comments. I hope you find the content useful in your future endeavors.

And to Jeff, special thanks to you sir for taking the time to slog through such a long article clearly not targeted at someone with your level of expertise. Your critical eye is always welcome of course. Keeps me honest.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search