2017-07-20 44 views
1

我正在构建一个报告以执行简单搜索工具来搜索VARCHAR(MAX)文本Blob。我试图让用户可以选择两种不同的搜索类型:包含所有搜索词,并包含任何搜索词。我无法找到可用于在包含all和Contains any之间进行更改的语法。以下是我希望如何工作的要点。谢谢您的帮助!在Transact-SQL中,如何更改包含all和包含任何使用参数

Where Reprt.DateStamp >= @StartDate and Reprt.DateStamp <= @EndDate and 
IF @SearchType = 'And' 
     Reprt.ContentText like '%'[email protected]+'%' and 
     Reprt.ContentText like '%'[email protected]+'%' and 
     Reprt.ContentText like '%'[email protected]+'%' and 
     Reprt.ContentText like '%'[email protected]+'%' 
Else 
     Reprt.ContentText like '%'[email protected]+'%' or 
     Reprt.ContentText like '%'[email protected]+'%' or 
     Reprt.ContentText like '%'[email protected]+'%' or 
     Reprt.ContentText like '%'[email protected]+'%' 
+1

错字遗憾意味着跨或Transact-SQL –

回答

2

使用AND/OR逻辑

Where Reprt.DateStamp >= @StartDate and Reprt.DateStamp <= @EndDate and 
((@SearchType = 'And' and 
     Reprt.ContentText like '%'[email protected]+'%' and 
     Reprt.ContentText like '%'[email protected]+'%' and 
     Reprt.ContentText like '%'[email protected]+'%' and 
     Reprt.ContentText like '%'[email protected]+'%') 
or (@SearchType <> 'And' 
     Reprt.ContentText like '%'[email protected]+'%' or 
     Reprt.ContentText like '%'[email protected]+'%' or 
     Reprt.ContentText like '%'[email protected]+'%' or 
     Reprt.ContentText like '%'[email protected]+'%')) 
+0

的或部分我不得不添加'和('后“和”,但比它完美的感谢其他您! –