2012-04-03 83 views
0

我正在研究一个高级搜索sproc,并想知道是否可能包含或不包含基于参数的部分选择项。我可以在我的参数上使用一个IF ELSE,但是我已经有一个用于另一个参数,并且这看起来像很多代码重复。SQL Server - 如何使INTERSECT选择可选?

- 我的高级搜索功能允许用户指定各种参数。基本搜索可以是EXACT或不包含(包含vs自由文本 - 第一个if)和其他参数(AND)可以指定。也可以选择特定关键字(相交)。

我的问题是,当@Keywords为null时,我不想在我的示例底部包含最终的INTERSECT SELECT ...部分代码。有没有快速的方法来做到这一点,而不是在顶部和底部查询中添加另一个IF ELSE?让我知道你是否需要更详细的信息。

declare @SearchTerms nvarchar(4000) 
declare @GalleryId int 
declare @Keywords nvarchar(4000) 
declare @ExactWord int 
declare @BeginDateUpload datetime 
declare @EndDateUpload datetime 
declare @BeginDateTaken datetime 
declare @EndDateTaken datetime 
declare @MinFileSize int 
declare @MaxFileSize int 
declare @AlbumType bit 
declare @ImageType int 
declare @AudioType int 
declare @OtherType int 
declare @VideoType int 

set @SearchTerms = 'tulips' 
set @GalleryId = 1 
set @Keywords = null -- 'st-jean' 
set @ExactWord = null 
set @BeginDateUpload = null 
set @EndDateUpload = null 
set @BeginDateTaken = null 
set @EndDateTaken = null 
set @MinFileSize = null 
set @MaxFileSize = null 
set @AlbumType = 1 
set @ImageType = 1 
set @AudioType = 1 
set @OtherType = 1 
set @VideoType = 1 
IF ISNULL(@ExactWord, 0) = 1 
    BEGIN 
      [... snip ...] 
    END 
ELSE 
    select t1.* from ( 
    SELECT 'm' as objType, m.MediaObjectId 
    FROM gs_mediaObjectMetadata md 
     INNER JOIN dbo.[gs_MediaObject] m 
     ON md.FKMediaObjectId = m.MediaObjectId 
     INNER JOIN dbo.[gs_Album] a 
     ON a.AlbumId = m.FKAlbumId 
     WHERE FREETEXT (value, @SearchTerms) 
     AND a.FKGalleryId = @GalleryId 
     AND (m.DateAdded >= ISNULL(@BeginDateUpload, m.DateAdded)) 
     AND (m.DateAdded <= ISNULL(@EndDateUpload, m.DateAdded)) 
     AND (m.DateTaken is NULL OR m.DateTaken >= ISNULL(@BeginDateTaken, m.DateTaken)) 
     AND (m.DateTaken is NULL OR m.DateTaken <= ISNULL(@EndDateTaken, m.DateTaken)) 
     AND (m.OriginalSizeKB >= ISNULL(@MinFileSize, m.OriginalSizeKB)) 
     AND (m.OriginalSizeKB <= ISNULL(@MaxFileSize, m.OriginalSizeKB)) 
     AND((m.FKMediaObjectTypeId = ISNULL(@ImageType, 0)) 
     OR (m.FKMediaObjectTypeId = ISNULL(@AudioType, 0)) 
     OR (m.FKMediaObjectTypeId = ISNULL(@VideoType, 0)) 
     OR (m.FKMediaObjectTypeId = ISNULL(@OtherType, 0))) 



union 

     SELECT 'm' as objType, m.MediaObjectId 
FROM dbo.[gs_MediaObject] m 
     INNER JOIN dbo.[gs_Album] a 
     ON a.AlbumId = m.FKAlbumId 
     WHERE FREETEXT ((m.Title, OriginalFilename), @SearchTerms) 
     AND a.FKGalleryId = @GalleryId 
     AND (m.DateAdded >= ISNULL(@BeginDateUpload, m.DateAdded)) 
     AND (m.DateAdded <= ISNULL(@EndDateUpload, m.DateAdded)) 
     AND (m.DateTaken is NULL OR m.DateTaken >= ISNULL(@BeginDateTaken, m.DateTaken)) 
     AND (m.DateTaken is NULL OR m.DateTaken <= ISNULL(@EndDateTaken, m.DateTaken)) 
     AND (m.OriginalSizeKB >= ISNULL(@MinFileSize, m.OriginalSizeKB)) 
     AND (m.OriginalSizeKB <= ISNULL(@MaxFileSize, m.OriginalSizeKB)) 
     AND((m.FKMediaObjectTypeId = ISNULL(@ImageType, 0)) 
     OR (m.FKMediaObjectTypeId = ISNULL(@AudioType, 0)) 
     OR (m.FKMediaObjectTypeId = ISNULL(@VideoType, 0)) 
     OR (m.FKMediaObjectTypeId = ISNULL(@OtherType, 0))) 
    ) t1 
--IF @Keywords != null -- conditional intersect 
intersect 

     SELECT 'm' as objType, m.MediaObjectId 
FROM dbo.[gs_MediaObject] m 
     INNER JOIN dbo.[gs_Album] a 
     ON a.AlbumId = m.FKAlbumId 
     left join dbo.gs_MediaObjectMetadata md 
     on m.MediaObjectId = md.FKMediaObjectId 
     WHERE FREETEXT ((m.Title, OriginalFilename), @SearchTerms) 
     AND a.FKGalleryId = @GalleryId 
     AND (m.DateAdded >= ISNULL(@BeginDateUpload, m.DateAdded)) 
     AND (m.DateAdded <= ISNULL(@EndDateUpload, m.DateAdded)) 
     AND (m.DateTaken is NULL OR m.DateTaken >= ISNULL(@BeginDateTaken, m.DateTaken)) 
     AND (m.DateTaken is NULL OR m.DateTaken <= ISNULL(@EndDateTaken, m.DateTaken)) 
     AND (m.OriginalSizeKB >= ISNULL(@MinFileSize, m.OriginalSizeKB)) 
     AND (m.OriginalSizeKB <= ISNULL(@MaxFileSize, m.OriginalSizeKB)) 
     AND((m.FKMediaObjectTypeId = ISNULL(@ImageType, 0)) 
     OR (m.FKMediaObjectTypeId = ISNULL(@AudioType, 0)) 
     OR (m.FKMediaObjectTypeId = ISNULL(@VideoType, 0)) 
     OR (m.FKMediaObjectTypeId = ISNULL(@OtherType, 0))) 

and UPPER(md.Description) = 'KEYWORDS' 
     and exists (
       SELECT * 
       FROM dbo.fnSplit(Replace(md.Value, '''', ''''''), ',') split 
       WHERE split.item in 
       (SELECT * from dbo.fnSplit(Replace(@Keywords, '''', ''''''), ',')) 
     ) 

谢谢

回答

0

什么,而不是做相交,尝试了这一点在你内心的疑问:@Keyword为空或.....

 and (@Keywords is null or (UPPER(md.Description) = 'KEYWORDS' 
     and exists (
       SELECT * 
       FROM dbo.fnSplit(Replace(md.Value, '''', ''''''), ',') split 
       WHERE split.item in 
       (SELECT * from dbo.fnSplit(Replace(@Keywords, '''', ''''''), ',')) 
      ) 
     ) 
     ) 
+0

谢谢你的提示,它并没有像代码那样工作,但我会继续调整,看看我能否实现它 – crichard 2012-04-03 15:45:34