2012-04-03 96 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