是否可以将 CASE 与 IN 一起使用?

发布于 2024-09-03 18:08:16 字数 219 浏览 3 评论 0原文

我正在尝试使用由输入参数确定的 WHERE 子句构建 T-SQL 语句。比如:

SELECT * FROM table
WHERE id IN
CASE WHEN @param THEN
(1,2,4,5,8)
ELSE
(9,7,3)
END

我已经尝试了所有我能想到的移动 IN、CASE 等的组合。这(或类似的事情)可能吗?

I'm trying to construct a T-SQL statement with a WHERE clause determined by an input parameter. Something like:

SELECT * FROM table
WHERE id IN
CASE WHEN @param THEN
(1,2,4,5,8)
ELSE
(9,7,3)
END

I've tried all combination of moving the IN, CASE etc around that I can think of. Is this (or something like it) possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2024-09-10 18:08:16

试试这个:

SELECT * FROM table
WHERE (@param='??' AND id IN (1,2,4,5,8))
OR (@param!='??' AND id in (9,7,3))

使用索引会有问题。

动态搜索条件的关键是确保使用索引,而不是如何轻松地重用代码、消除查询中的重复项或尝试使用同一查询执行所有操作。这里有一篇关于如何处理这个主题的非常全面的文章:

T-SQL 中的动态搜索条件作者: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,则可以使用其他技术,请参阅:SQL 2008(SP1 CU5 及更高版本)的 T-SQL 版本中的动态搜索条件

如果您使用的是 SQL Server 2008 的正确版本,则只需添加 OPTION (RECOMPILE) 到查询,运行时局部变量的值用于优化。

考虑到这一点,OPTION (RECOMPILE) 将采用此代码(其中没有索引可以与这些混乱的 OR 一起使用):

WHERE
    (@search1 IS NULL or Column1=@Search1)
    AND (@search2 IS NULL or Column2=@Search2)
    AND (@search3 IS NULL or Column3=@Search3)

并在运行时对其进行优化(前提是只有 @Search2 传入了一个值):

WHERE
    Column2=@Search2

并且可以使用索引(如果您在 Column2 上定义了索引)

try this:

SELECT * FROM table
WHERE (@param='??' AND id IN (1,2,4,5,8))
OR (@param!='??' AND id in (9,7,3))

this will have a problem using an index.

The key with a dynamic search conditions is to make sure an index is used, instead of how can I easily reuse code, eliminate duplications in a query, or try to do everything with the same query. Here is a very comprehensive article on how to handle this topic:

Dynamic Search Conditions in T-SQL by Erland Sommarskog

It covers all the issues and methods of trying to write queries with multiple optional search conditions. This main thing you need to be concerned with is not the duplication of code, but the use of an index. If your query fails to use an index, it will preform poorly. There are several techniques that can be used, which may or may not allow an index to be used.

here is the table of contents:

  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

if you are on the proper version of SQL Server 2008, there is an additional technique that can be used, see: Dynamic Search Conditions in T-SQL Version for SQL 2008 (SP1 CU5 and later)

If you are on that proper release of SQL Server 2008, you can just add OPTION (RECOMPILE) to the query and the local variable's value at run time is used for the optimizations.

Consider this, OPTION (RECOMPILE) will take this code (where no index can be used with this mess of ORs):

WHERE
    (@search1 IS NULL or Column1=@Search1)
    AND (@search2 IS NULL or Column2=@Search2)
    AND (@search3 IS NULL or Column3=@Search3)

and optimize it at run time to be (provided that only @Search2 was passed in with a value):

WHERE
    Column2=@Search2

and an index can be used (if you have one defined on Column2)

橘味果▽酱 2024-09-10 18:08:16
if @param = 'whatever'
   select * from tbl where id in (1,2,4,5,8)
else
   select * from tbl where id in (9,7,3)
if @param = 'whatever'
   select * from tbl where id in (1,2,4,5,8)
else
   select * from tbl where id in (9,7,3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文