在Transact-SQL中,如何使用参数

时间:2017-07-20 14:54:49

标签: sql sql-server tsql

我正在构建一个报告,以便使用简单的搜索工具来搜索VARCHAR(MAX)文本blob。我试图让用户可以选择两种不同的搜索类型:包含所有搜索词,包含任何搜索词。我无法找到适用于在包含所有内容和包含任何内容之间进行更改的语法。以下是我希望它如何工作的要点。谢谢你的帮助!

Where Reprt.DateStamp >= @StartDate and Reprt.DateStamp <= @EndDate and
IF @SearchType = 'And'
        Reprt.ContentText like '%'+@SearchString1+'%' and
        Reprt.ContentText like '%'+@SearchString2+'%' and 
        Reprt.ContentText like '%'+@SearchString3+'%' and 
        Reprt.ContentText like '%'+@SearchString4+'%'
Else
        Reprt.ContentText like '%'+@SearchString1+'%' or
        Reprt.ContentText like '%'+@SearchString2+'%' or 
        Reprt.ContentText like '%'+@SearchString3+'%' or 
        Reprt.ContentText like '%'+@SearchString4+'%'

1 个答案:

答案 0 :(得分:2)

使用AND/OR逻辑

Where Reprt.DateStamp >= @StartDate and Reprt.DateStamp <= @EndDate and
((@SearchType = 'And' and 
        Reprt.ContentText like '%'+@SearchString1+'%' and
        Reprt.ContentText like '%'+@SearchString2+'%' and 
        Reprt.ContentText like '%'+@SearchString3+'%' and 
        Reprt.ContentText like '%'+@SearchString4+'%')
or (@SearchType <> 'And'
        Reprt.ContentText like '%'+@SearchString1+'%' or
        Reprt.ContentText like '%'+@SearchString2+'%' or 
        Reprt.ContentText like '%'+@SearchString3+'%' or 
        Reprt.ContentText like '%'+@SearchString4+'%'))