我在桌子上有7个字段;可能的字段组合会产生一些结果。组合可以是1或4或7或全部等等,可以根据客户端的指示动态更改。我必须为它创建一个程序。这些字段是BIT类型。有没有其他方法可以创建这些程序,除非通过If Else定义每个案例...如果还有?
@Chk_title AS BIT ,
@Chk_Description AS Bit ,
@Chk_Keywords AS BIT,
@Chk_Category AS BIT,
@Chk_Location AS BIT,
@Chk_Source AS BIT,
@Chk_Date AS BIT,
@RD_btn_AND_OR AS BIT
AS
if @RD_btn_AND_OR = 1
Begin
if @Chk_title = 1 AND @Chk_Description=1 AND @Chk_Keywords=1 @Chk_Category=1 @ Location=1 @ Chk_Source=1 @Chk_Date=1
Begin
Select title, Description, Keywords, category, Location, source, Date from server_des where title Like '%'+@title+'%' AND Description Like '%'+@Description+'%' AND Keywords Like '%'+@Keywords+'%' AND category Like '%'+@Category+'%' AND Location Like '%'+@Location+'%'AND source Like '%'+@Source+'%' AND Date Like '%'+@Date+'%'
End
ELSE if @Chk_title = 1 AND @Chk_Description = 1 AND @Chk_Keywords = 0 @Chk_Category=1 @ Location=1 @ Chk_Source=1 @Chk_Date=0
Begin
Select title, Description, null as'Keywords', category, Location, source, Date from server_des where title Like '%'+@title+'%' AND Description Like '%'+@Description+'%' AND category Like '%'+@Category+'%' AND Location Like '%'+@Location+'%' AND source Like '%'+@Source+'%' AND Date Like '%'+@Date+'%'
End
ELSE if @Chk_title=0 AND @Chk_Description=1 AND @Chk_Keywords=1 @Chk_Category=1 @ Location=1 @ Chk_Source=0 @Chk_Date=1
Begin
Select null as'Title', Description, Keywords, category, Location, source, Date from server_des where Description Like '%'+@Description+'%' AND Keywords Like '%'+@Keywords+'%' AND category Like '%'@Category'%' AND Location Like '%'+@Location+'%' AND source Like '%'+@Source+'%' AND Date Like '%'+@Date+'%'
End
END
答案 0 :(得分:1)
你可以将它全部放在这样的一个语句中,但是你应该检查以确保创建的执行计划并不比旧的解决方案差得多(如果性能对于这个存储的proc很重要):
SELECT title, Description, Keywords, category, Location, source, Date
FROM server_des
WHERE (@Chk_title <> 1 OR title LIKE '%'+@title+'%')
AND (@Chk_Description <> 1 OR Description LIKE '%'+@Description+'%')
AND (@Chk_Keywords <> 1 OR Keywords Like '%'+@Keywords+'%')
AND (@Chk_Category <> 1 OR category Like '%'+@Category+'%')
AND (@Chk_Location <> 1 OR Location Like '%'+@Location+'%')
AND (@Chk_Source <> 1 OR source Like '%'+@Source+'%')
AND (@Chk_Data <> 1 OR Date Like '%'+@Date+'%)
答案 1 :(得分:1)
您可以使用动态SQL - 根据您的数据撰写SQL命令并运行它。像这样:
@Chk_title AS BIT ,
@Chk_Description AS Bit ,
@Chk_Keywords AS BIT,
@Chk_Category AS BIT,
@Chk_Location AS BIT,
@Chk_Source AS BIT,
@Chk_Date AS BIT,
@RD_btn_AND_OR AS BIT
AS
DECLARE @cmdSQL VARCHAR(250)
SET @cmdSQL = 'Select'
if @RD_btn_AND_OR = 1
Begin
if @Chk_title <> 1
SET @cmdSQL = @cmdSQL + ' null as'
SET @cmdSQL = @cmdSQL + ' title,'
if @Chk_Description <> 1
SET @cmdSQL = @cmdSQL + ' null as'
SET @cmdSQL = @cmdSQL + ' Description,'
if @Chk_Keywords <> 1
SET @cmdSQL = @cmdSQL + ' null as'
SET @cmdSQL = @cmdSQL + ' Keywords,'
if @Chk_Category <> 1
SET @cmdSQL = @cmdSQL + ' null as'
SET @cmdSQL = @cmdSQL + ' Category,'
if @Chk_Location <> 1
SET @cmdSQL = @cmdSQL + ' null as'
SET @cmdSQL = @cmdSQL + ' Location,'
if @Chk_Source <> 1
SET @cmdSQL = @cmdSQL + ' null as'
SET @cmdSQL = @cmdSQL + ' Source,'
if @Chk_Date <> 1
SET @cmdSQL = @cmdSQL + ' null as'
SET @cmdSQL = @cmdSQL + ' Date'
End
SET @cmdSQL = @cmdSQL + ' from server_des where title Like ''%''+@title+''%'' AND Description Like ''%''+@Description+''%'' AND Keywords Like ''%''+@Keywords+''%'' AND category Like ''%''+@Category+''%'' AND Location Like ''%''+@Location+''%''AND source Like ''%''+@Source+''%'' AND Date Like ''%''+@Date+''%'''
EXEC(@cmdSQL)
END