|
2
|
| dkackman Srinivas Kokkula · 技术社区 · 16 年前 |
|
1
7
试试这个:
使用索引时会出现问题。 使用动态搜索条件的关键是确保使用索引,而不是如何轻松地重用代码、消除查询中的重复或尝试使用同一查询执行所有操作。下面是一篇非常全面的文章,介绍如何处理这个问题: Dynamic Search Conditions in T-SQL by Erland Sommarskog 它涵盖了尝试使用多个可选搜索条件编写查询的所有问题和方法。您需要关注的主要问题不是代码的重复,而是索引的使用。如果您的查询没有使用索引,它的执行效果会很差。有几种技术可以使用,可能允许也可能不允许使用索引。 目录如下: 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
如果您使用的是正确版本的SQL Server 2008,则可以使用其他技术,请参阅: Dynamic Search Conditions in T-SQL Version for SQL 2008 (SP1 CU5 and later)
如果您使用的是正确的SQLServer2008版本,那么只需添加
想想这个,
并在运行时将其优化为(前提是只有@Search2传递了一个值):
并且可以使用索引(如果在第2列中定义了索引) |
|
|
2
0
|
|
|
sqlrobert · SQL-如何查找不包含特定字段值的一组记录 2 年前 |
|
|
Nick Fleetwood · 调度语法的LINQ查询 3 年前 |
|
|
Mateen Bagheri · 选择表的计数并选择其自身 3 年前 |
|
Java · 使用交叉应用同时显示两列 4 年前 |