我正在研究高级搜索存储过程,并想知道是否可以根据参数包含我的选择的一部分。我可以在我的参数上使用 IF ELSE,但我已经有一个用于另一个参数,这似乎有很多代码重复。
-- 我的高级搜索功能允许用户指定各种参数。基本搜索可以是 EXACT 也可以不是(包含 vs freetext - first if),并且可以指定其他参数(AND)。也可以选择特定的关键字(相交)。
我的问题是,当 @Keywords 为空时,我不想在示例底部包含最后的 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, '''', ''''''), ','))
)
谢谢