我在存储过程中使用CASE语句。我正在使用像
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 @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
我想在这个'关键字'上检查更多条件,例如当关键字和关键字不为空时 转到那个条件..
我使用WHEN 'keyword' AND (@keyword IS NULL) THEN ''
,但它的语法错误。
可以检查此类情况或以其他任何方式检查此
...谢谢
答案 0 :(得分:2)
用这个存储过程来判断你想要完成什么真的很难,但我认为你肯定比他们需要的更难。您可以将其重新编写为单个查询,如下所示:
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'''