如何将ELSE子句添加到以下代码中?
ROW_NUMBER() OVER (ORDER BY
CASE WHEN @OrderByColumn='ViewCount' AND @OrderDirection=0 THEN docsT.ViewCount END ASC,
CASE WHEN @OrderByColumn='AddedDate'AND @OrderDirection=0 THEN docsT.AddedDate END ASC,
CASE WHEN @OrderByColumn='AddedBy'AND @OrderDirection=0 THEN docsT.AddedBy END ASC,
CASE WHEN @OrderByColumn='Title' AND @OrderDirection=0 THEN docsT.Title END ASC
) AS RowNum
答案 0 :(得分:1)
您可以将订单放在外壳中,然后为了选择字段,您可以使用将一个值与多个值进行比较的情况的形式。要支持升序和降序排序,您必须重复这一切。
order by
case when @OrderDirection = 0 then
case @OrderByColumn
when 'ViewCount' then docsT.ViewCount
when 'AddedDate' then docsT.AddedDate
when 'AddedBy' then docsT.AddedBy
when 'Title' then docsT.Title
else docsT.Title
end
end asc,
case when @OrderDirection = 1 then
case @OrderByColumn
when 'ViewCount' then docsT.ViewCount
when 'AddedDate' then docsT.AddedDate
when 'AddedBy' then docsT.AddedBy
when 'Title' then docsT.Title
else docsT.Title
end
end desc
答案 1 :(得分:0)
只需在最后添加总是评估为true的内容。
我不知道你的要求是什么,所以我在猜测ORDER BY:
ROW_NUMBER() OVER (ORDER BY
CASE WHEN @OrderByColumn='ViewCount' AND @OrderDirection=0 THEN docsT.ViewCount END ASC,
CASE WHEN @OrderByColumn='AddedDate'AND @OrderDirection=0 THEN docsT.AddedDate END ASC,
CASE WHEN @OrderByColumn='AddedBy'AND @OrderDirection=0 THEN docsT.AddedBy END ASC,
CASE WHEN @OrderByColumn='Title' AND @OrderDirection=0 THEN docsT.Title END ASC,
CASE WHEN 1 = 1 THEN docsT.Title END ASC
) AS RowNum
答案 2 :(得分:0)
有很多方法:你可以将整个案例包装在IsNull(...,docsT.Title)
中。或者您可以使用ELSE
子句,例如:
CASE WHEN OrderDirection = 0 THEN
CASE @OrderByColumn
WHEN 'ViewCount' THEN docsT.ViewCount
WHEN 'AddedDate' THEN docsT.AddedDate
WHEN 'AddedBy' THEN docsT.AddedBy
ELSE docsT.Title
END
ELSE docsT.Title
END