select语句中的sql where子句问题

发布于 2024-09-10 05:38:47 字数 813 浏览 2 评论 0原文

我将 SQL Server 2008 Enterprise 与 Windows Server 2008 Enterprise 一起使用。我有一个名为“Book”的数据库表,其中有很多列,其中三列在这个问题中很重要,它们是

  • Author、varchar;
  • 国家/地区,varchar;
  • 域,varchar。

我想编写一个具有以下逻辑的存储过程,但我不知道如何编写(因为查询条件复杂),不胜感激,是否有人可以为我编写一个示例?

输入参数:p_author as varchar、p_country as varchar、p_domain as varchar

查询条件:

  1. 如果从输入中指定了 p_author,则任意行的 Author 列 LIKE %p_author% 满足条件,如果没有从输入中指定 p_author,则每行为对此条件感到满意;
  2. 如果从输入中指定了 p_country,则任何 Country 列 = p_country 的行都满足条件;如果没有从输入中指定 p_country,则每行都满足此条件;
  3. 如果从输入中指定了 p_domain,则满足其 Domain 列 LIKE %p_domain% 的任何行,如果未从输入中指定 p_domain,则每行都满足此条件;

我要返回的结果(必须满足以下所有条件):

  • 满足条件1或2的记录;
  • 记录必须满足条件3;
  • 返回不同的行。

例如,满足条件1和条件3的记录可以返回,满足条件2和条件3的记录可以返回。

提前致谢, 乔治

I am using SQL Server 2008 Enterprise with Windows Server 2008 Enterprise. I have a database table called "Book", which has many columns and three columns are important in this question, they are

  • Author, varchar;
  • Country, varchar;
  • Domain, varchar.

I want to write a store procedure with the following logics, but I do not know how to write (because of complex query conditions), appreciate if anyone could write a sample for me?

Input parameter: p_author as varchar, p_country as varchar, and p_domain as varchar

Query conditions:

  1. if p_author is specified from input, then any row whose Author column LIKE %p_author% is satisfied with condition, if p_author is not specified from input every row is satisfied with this condition;
  2. if p_country is specified from input, then any row whose Country column = p_country is satisfied with condition, if p_country is not specified from input every row is satisfied with this condition;
  3. if p_domain is specified from input, then any row whose Domain column LIKE %p_domain% is satisfied, if p_domain is not specified from input every row is satisfied with this condition;

The results I want to return (must met with all following conditions):

  • records met with either condition 1 or 2;
  • records must meet with condition 3;
  • return distinct rows.

For example, records which met with condition 1 and condition 3 are ok to return, and records which met with condition 2 and condition 3 are ok to return.

thanks in advance,
George

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

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

发布评论

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

评论(2

太傻旳人生 2024-09-17 05:38:47

根据给定参数动态改变搜索是一个复杂的主题,并且以一种不同的方式进行,即使只有非常微小的差异,也会产生巨大的性能影响。关键是使用索引,忽略紧凑的代码,忽略担心重复代码,必须制定好的查询执行计划(使用索引)。

阅读本文并考虑所有方法。您的最佳方法将取决于您的参数、数据、模式和实际使用情况:

动态搜索《T-SQL 条件》,作者:Erland Sommarskog

动态 SQL 的诅咒与祝福,作者:Erland Sommarskog< /a>

如果您有正确的 SQL Server 2008 版本(SQL 2008 SP1 CU5 (10.0.2746) 及更高版本),您可以使用这个小技巧来实际使用索引:

由于您正在使用索引,因此您无能为力LIKE,但如果您使用相等,则可以在查询中添加 OPTION (RECOMPILE),请参阅 Erland 的文章,SQL Server 将在 (Column = @Param+'% ' OR @Param='') AND ... 在根据局部变量的运行时值创建查询计划之前,如果您没有使用 LIKE,则可以使用索引

Dynamically changing searches based on the given parameters is a complicated subject and doing it one way over another, even with only a very slight difference, can have massive performance implications. The key is to use an index, ignore compact code, ignore worrying about repeating code, you must make a good query execution plan (use an index).

Read this and consider all the methods. Your best method will depend on your parameters, your data, your schema, and your actual usage:

Dynamic Search Conditions in T-SQL by by Erland Sommarskog

The Curse and Blessings of Dynamic SQL by Erland Sommarskog

If you have the proper SQL Server 2008 version (SQL 2008 SP1 CU5 (10.0.2746) and later), you can use this little trick to actually use an index:

There isn't much you can do since you are using LIKE, but if you were using equality, you could add OPTION (RECOMPILE) onto your query, see Erland's article, and SQL Server will resolve the OR from within (Column = @Param+'%' OR @Param='') AND ... before the query plan is created based on the run-time values of the local variables, and an index can be used if you weren't using LIKE.

爱她像谁 2024-09-17 05:38:47

如果我理解正确,以下应该有效:

 SELECT * 
FROM Books
WHERE (
   ((Author LIKE '%' + @p_author + '%' OR @p_author = '') OR 
   (Country LIKE '%' + @p_country + '%' OR @p_country = ''))
   AND (@p_author <> '' OR @p_country <> '')
) AND 
(Domain LIKE '%' + @p_domain + '%' OR '%' @p_domain = '')

If I understand correctly, the following should work:

 SELECT * 
FROM Books
WHERE (
   ((Author LIKE '%' + @p_author + '%' OR @p_author = '') OR 
   (Country LIKE '%' + @p_country + '%' OR @p_country = ''))
   AND (@p_author <> '' OR @p_country <> '')
) AND 
(Domain LIKE '%' + @p_domain + '%' OR '%' @p_domain = '')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文