开发者

How to Check IF condition in Case statement of Stored procedure

I am using CASE Statement in Stored procedure. I am using like

create proc USP_new
(
@searchtype varchar(30),
@stateName char(2),
    @keywords varchar(300),
    @locations varchar(100),
    @searchBy varchar(20),
    @keywordOption varchar(5),
    @jobType char(4),
    @startDate varchar(20),
    @endDate varchar(20),
    @companyID int

 ) 
 as begin
 declare @mainQuery varchar(8000)
SELECT @mainQuery = 'SELECT JobID, JobTitle, CompanyInfo, JobSummaryLong, ModifiedDate 
                 FROM Jobs 
                 WHERE IsActive = 1 '
    IF @开发者_Python百科startDate = '' BEGIN
        SELECT @mainQuery = @mainQuery + 'And ModifiedDate >= ''' + @startDate + ' 00:00:00'' '
    END
    SELECT @mainQuery = @mainQuery + 'And ModifiedDate <= ''' + @endDate + ' 23:59:59'''
SELECT
CASE  @mainQuery
WHEN 'state'    THEN 'ONE'
WHEN 'keyword' THEN 'Second'
WHEN 'company' THEN 'Third'
ELSE 'Other'

END

I want check more condition on this 'Keyword' like When Keyword and keyword is not null then goto THEN condition..

I used like WHEN 'keyword' AND (@keyword IS NULL) THEN '' but its is giving syntax error.

IT is possible to check condition like this or any other way to check this

Thanks....


It's really hard to tell what you are trying to accomplish with this stored proc, but I think you are definitely making thing harder than they need to be. You could probably re-write this as a single query like so:

SELECT 
    KeywordResult = CASE 
        WHEN @keywords = 'state' THEN 'ONE'
        WHEN @keywords = 'keyword' THEN 'Second'
        WHEN @keywords = 'company' THEN 'Third'
        ELSE 'Other' END,
    JobID, 
    JobTitle, 
    CompanyInfo, 
    JobSummaryLong, 
    ModifiedDate 
 FROM 
    Jobs 
 WHERE 
    IsActive = 1
    AND (@StartDate <> '' AND ModifiedDate >= @StartDate)
    AND ModifiedDate <= @endDate + ' 23:59:59'''
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜