下面的存储过程工作正常,除了当我在“where”子句中取消注释日期检查的第二部分时,即使传入的关键字为空或“111”,它也会在日期转换时爆炸。
我愿意接受有关如何以不同方式执行此动态 where 子句的任何建议。
我很感激任何帮助。
ALTER PROCEDURE [SurveyEngine].[GetPageOf_CommentsOverviewRowModel]
@sortColumn varchar(50),
@isASC bit,
@keyword varchar(50)
AS
BEGIN
declare @keywordType varchar(4)
set @keywordType = null
if ISDATE(@keyword) = 1
set @keywordType = 'date'
else if ISNUMERIC(@keyword) = 1
set @keywordType = 'int'
select c.CommentBatch BatchID, c.CreatedDate DateReturned, COUNT(c.CommentID) TotalComments
from SurveyEngine.Comment c
where (@keywordType is null)
or (@keywordType = 'date') --and c.CreatedDate = @keyword)
or (@keywordType = 'int' and (CONVERT(varchar(10), c.CommentBatch) like @keyword+'%'))
group by c.CommentBatch, c.CreatedDate
order by case when @sortColumn = 'BatchID' and @isASC = 0 then c.CommentBatch end desc,
case when @sortColumn = 'BatchID' and @isASC = 1 then c.CommentBatch end,
case when @sortColumn = 'DateReturned' and @isASC = 0 then c.CreatedDate end desc,
case when @sortColumn = 'DateReturned' and @isASC = 1 then c.CreatedDate end,
case when @sortColumn = 'TotalComments' and @isASC = 0 then COUNT(c.CommentID) end desc,
case when @sortColumn = 'TotalComments' and @isASC = 1 then COUNT(c.CommentID) end
END