开发者

Is there any other way for this procedure?

I have 7 fields in a table; possible combinations of fields generate some result. A combination can be 1 or 4 or 7 or all and so on, which can be changed dynamically as given by client. I have to create a procedure for it. These fields are BIT type. Is there any other way to create these procedures except defining for each case by If Else...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 Lik开发者_Go百科e '%'+@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


You can use dynamic SQL - compose SQL command based on your data and run it. Like this:

@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


You could put it all in one statement like this, but you should check to make sure the execution plan that gets created isn't significantly worse than your old solution (if performance is important for this stored 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+'%)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜