SELECT
Column1,....
FROM MyTable
WHERE MyTable.StartDate>=COALESCE(@startDate,CONVERT(datetime,'01/01/1753'))
AND MyTable.EndDate<=COALESCE(@endDate,CONVERT(datetime,'12/31/9999'))
anche, ecco un articolo molto esauriente su questo argomento:
Dynamic Search Conditions in T-SQL by Erland Sommarskog
che copre tutte le questioni e le modalità di cercare di scrivere query con più condizioni di ricerca opzionali
qui è il sommario:
Introduction
The Case Study: Searching Orders
The Northgale Database
Dynamic SQL
Introduction
Using sp_executesql
Using the CLR
Using EXEC()
When Caching Is Not Really What You Want
Static SQL
Introduction
x = @x OR @x IS NULL
Using IF statements
Umachandar's Bag of Tricks
Using Temp Tables
x = @x AND @x IS NOT NULL
Handling Complex Conditions
Hybrid Solutions – Using both Static and Dynamic SQL
Using Views
Using Inline Table Functions
Conclusion
Feedback and Acknowledgements
Revision History
fonte
2009-10-05 15:49:03
La prego di inviare quale sarebbe la vostra richiesta simile da entrambi i parametri definiti? – Quassnoi