Sql Server 条件包含 Null 参数的自由文本搜索处理

发布于 2024-12-05 16:47:05 字数 875 浏览 0 评论 0原文

为了让这个查询继续进行,我已经努力了很长一段时间。

简而言之,我的查询通过 fileno 和/或搜索字段进行搜索

DECLARE @pSearchFor AS NVARCHAR(100);

- 我在这里使用 null 值、 ' ' 或单独的单词进行测试 SET @pSearchFor = null -- '“marsa”和“mosta”';

 IF ISNULL(@pSearchFor,'') = '' SET @pSearchFor = '""' ;

declare @fileNo nvarchar(50) = 'e/e'

select top 1000 r.FileId, r.FileNo,  fs.SearchField, @pSearchFor

from regfile as r
left outer join FileSearchFields as fs on r.FileId = fs.FileID
where r.FileNo like 
CASE 
 WHEN  Len(@fileno) > 1 THEN '%'+@fileNo+'%'
 ELSE r.FileNo
END

 AND 
     1 = 
     CASE WHEN ISNULL(@pSearchFor, '') = '' THEN 1 ELSE 0 END
       or CONTAINS(fs.SearchField, @pSearchFor)

如果 @pSearchFor 为 null,我不会返回任何内容,否则效果很好。

如果 null ,我需要返回所有实例

一种可能的解决方案可能是调用 2 个单独的 sps 或使用 if /else 但可能存在更好的方法。

我真的很感谢你的帮助!

I have been struggling for quite some time to get this query going.

In short my query searches by fileno and/or searchfield

DECLARE @pSearchFor AS NVARCHAR(100);

-- I am here testing with null value, ' ' , or seperate words
SET @pSearchFor = null -- '"marsa" and "mosta"';

 IF ISNULL(@pSearchFor,'') = '' SET @pSearchFor = '""' ;

declare @fileNo nvarchar(50) = 'e/e'

select top 1000 r.FileId, r.FileNo,  fs.SearchField, @pSearchFor

from regfile as r
left outer join FileSearchFields as fs on r.FileId = fs.FileID
where r.FileNo like 
CASE 
 WHEN  Len(@fileno) > 1 THEN '%'+@fileNo+'%'
 ELSE r.FileNo
END

 AND 
     1 = 
     CASE WHEN ISNULL(@pSearchFor, '') = '' THEN 1 ELSE 0 END
       or CONTAINS(fs.SearchField, @pSearchFor)

I am getting nothing returned if @pSearchFor is null otherwise works great.

I need to return all instances if a null

One possible solution might be to call 2 seperate sps or use if /else but probably exists a better method.

I really do appreciate your help!

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

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

发布评论

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

评论(2

南风起 2024-12-12 16:47:05

首先,将 @pSearchFor 设置为 ""

IF ISNULL(@pSearchFor,'') = '' SET @pSearchFor = '""' ;

这意味着这永远不会返回 1:

CASE WHEN ISNULL(@pSearchFor, '') = '' THEN 1 ELSE 0 END

您需要使用不同的变量,或者使用相同类型的 CASE select 列表中的 表达式,而不是将值从 NULL 更改为 ""

SELECT TOP 1000 r.FileId, r.FileNo,  fs.SearchField, 
    CASE WHEN COALESCE(@pSearchFor, '') = '' THEN '""' ELSE @pSearchFor END

此外,您还使用 SELECT TOP 但没有 ORDER BY ...如果您想要一个子集,您不关心获得哪个子集吗?

First you set @pSearchFor to "":

IF ISNULL(@pSearchFor,'') = '' SET @pSearchFor = '""' ;

That means this will never return 1:

CASE WHEN ISNULL(@pSearchFor, '') = '' THEN 1 ELSE 0 END

You need to either use a different variable, or use the same type of CASE expression in the select list, instead of changing the value from NULL to "".

SELECT TOP 1000 r.FileId, r.FileNo,  fs.SearchField, 
    CASE WHEN COALESCE(@pSearchFor, '') = '' THEN '""' ELSE @pSearchFor END

Also you use SELECT TOP but no ORDER BY ... if you want a subset, don't you care which subset you get?

泪冰清 2024-12-12 16:47:05

我已经解决了这个问题。也许这对其他人有帮助!
这是我的存储过程的一个片段。

 @fileNo nvarchar(50) = null ,
 @fields nvarchar(100) = '""',`enter code here`
 @datefrom date = null,
 @dateto date  = null,
...
AS`enter code here`
BEGIN

 if (@fields = null or LEN(@fields) < 1 ) set @fields = '""'

select top 1000 r.*,
(CASE 
        WHEN fs.SearchField IS NULL THEN CONVERT(NVarChar(1),'')
        ELSE CONVERT(NVarChar(MAX),fs.SearchField)
     END) AS [Search]  
from regfile as r
left outer join FileSearchFields as fs on r.FileId = fs.FileID
where r.FileNo like 
CASE 
 WHEN  Len(@fileno) > 1 THEN '%'+@fileNo+'%'
 ELSE r.FileNo
 END
 and
  r.Date between 
CASE 
 WHEN  @datefrom != '' THEN @datefrom
 ELSE '1900-1-1'
 END

 and 
CASE 
 WHEN  @dateto != '' THEN @dateto
 ELSE '9999-1-1'
 END
 and 
 ((LEN(@fields) > 2 and contains(fs.SearchField,@fields))or (LEN(@fields) <= 2)) 
 --NB: <= 2 as we have added the "" characters in @fields!

end

I have solved the problem. Maybe this may be of some help to others!
This is a snippet of my stored procedure.

 @fileNo nvarchar(50) = null ,
 @fields nvarchar(100) = '""',`enter code here`
 @datefrom date = null,
 @dateto date  = null,
...
AS`enter code here`
BEGIN

 if (@fields = null or LEN(@fields) < 1 ) set @fields = '""'

select top 1000 r.*,
(CASE 
        WHEN fs.SearchField IS NULL THEN CONVERT(NVarChar(1),'')
        ELSE CONVERT(NVarChar(MAX),fs.SearchField)
     END) AS [Search]  
from regfile as r
left outer join FileSearchFields as fs on r.FileId = fs.FileID
where r.FileNo like 
CASE 
 WHEN  Len(@fileno) > 1 THEN '%'+@fileNo+'%'
 ELSE r.FileNo
 END
 and
  r.Date between 
CASE 
 WHEN  @datefrom != '' THEN @datefrom
 ELSE '1900-1-1'
 END

 and 
CASE 
 WHEN  @dateto != '' THEN @dateto
 ELSE '9999-1-1'
 END
 and 
 ((LEN(@fields) > 2 and contains(fs.SearchField,@fields))or (LEN(@fields) <= 2)) 
 --NB: <= 2 as we have added the "" characters in @fields!

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