SQL Server Express 2008 R2 - 文本字段中的方括号不返回结果

发布于 2024-10-08 09:52:58 字数 455 浏览 0 评论 0原文

我创建了一个带有一些默认值的简单存储过程,以启用“MachineModel”数据库的自定义搜索。

作为 WHERE 子句的一部分,我有这样的内容:

Model.[ModelName] LIKE '%' + ISNULL(@ModelName, Model.[ModelName]) + '%' 

它应该允许部分匹配。这适用于大多数模型,但是如果没有为 @ModelName 提供任何值(因此 ISNULL 解析为 Model.[ModelName])并且底层字段数据包含方括号,例如“Lenny's Test Model [2010]”,那么 SProc 不会返回这些记录。

这并不是一个大问题,因为实际上只有 4 个模型(约 120,000 个)的名称中包含方括号,因此我可以轻松更改它们,但是我很想知道发生了什么以及解决此问题的最佳方法是什么。

干杯,

莱尼。

I've created a simple stored procedure with some default values to enable customised searching of my "MachineModel" database.

As part of the WHERE clause I have something like this:

Model.[ModelName] LIKE '%' + ISNULL(@ModelName, Model.[ModelName]) + '%' 

which should allow for partial matches. This works for the majority of models, however if no value is supplied for @ModelName (and therefore the ISNULL resolves to Model.[ModelName]) and the underlying field data contains square brackets, such as "Lenny's Test Model [2010]", then those records records aren't returned by the SProc.

This isn't a huge problem as only 4 models (of about 120,000) actually have square brackets in their names so I could easily change them, however I'm curious to know what's going on and what's the best way to solve this.

Cheers,

Lenny.

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

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

发布评论

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

评论(2

撩心不撩汉 2024-10-15 09:52:58

您需要将前括号替换为 [[]

LIKE 中有 3 个有意义的字符:% _ [。您不需要转义尾括号。

为了完整性:

Model.[ModelName] LIKE '%' + ISNULL(
    --don't do it the other way around!
    REPLACE(REPLACE(REPLACE(@ModelName, '[', '[[]), '%', '[%]), '_', '[_]),
    Model.[ModelName]) + '%'

You need to replace the leading bracket by [[]

There are 3 characters with meaning in LIKE: % _ [. You don't need to escape the trailing bracket.

For completeness:

Model.[ModelName] LIKE '%' + ISNULL(
    --don't do it the other way around!
    REPLACE(REPLACE(REPLACE(@ModelName, '[', '[[]), '%', '[%]), '_', '[_]),
    Model.[ModelName]) + '%'
难如初 2024-10-15 09:52:58

为什么在搜索参数为空的情况下将模型与其自身进行比较?这很贵。

将 and 和 or 与 SQL Server 混合使用也不是很好的做法,因为它很昂贵,但可以这样做:

Where (
 @ModelName is null OR
 (ModelName is not null AND ModelName LIKE '%' + @ModelName + '%')
)

如果性能可能是一个问题,那么引入 if 可以有所帮助,但最终会重复您的选择

if @ModelName is null 
 select * from ...
else
 select ...
 where ModelName LIKE '%' + @ModelName + '%'

您可以,但它会让你的 dba 不高兴,在代码中构建 sql 并使用 sqlparameters,并且仅在该 var 不为空时添加 where

Why are you comparing the model with itself in case of null search parameter? This is expensive.

It is also not great practice to mix ands and ors with SQL Server as its expensive, but could be done:

Where (
 @ModelName is null OR
 (ModelName is not null AND ModelName LIKE '%' + @ModelName + '%')
)

if performance could be a prob, then introducing an if can help, but you do end up duplicating your select

if @ModelName is null 
 select * from ...
else
 select ...
 where ModelName LIKE '%' + @ModelName + '%'

You could, but it will upset your dba, build the sql up in code and use sqlparameters and only add the where when this var is not null

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文