我正在使用SQL Server 2008.
SELECT resultTable.OrderNumber,
resultTable.ProjectId,
resultTable.BatchId,
resultTable.CustomerId,
resultTable.City,
resultTable.Street,
resultTable.PostalCode,
resultTable.Country,
resultTable.CreatedDate,
resultTable.Name,
COUNT(*) OVER() as OrdersCount,
Row_Number() OVER
(ORDER BY
CASE WHEN @sortBy = 'OrderNumber'
THEN resultTable.OrderNumber END,
CASE WHEN @sortBy = 'ProjectId'
THEN resultTable.ProjectId END,
CASE WHEN @sortBy = 'Address'
THEN resultTable.Country, resultTable.City, resultTable.Street, resultTable.PostalCode END,
CASE WHEN @sortBy = 'CreatedDate'
THEN resultTable.CreatedDate END) as RowIndex
FROM resultTable
此查询在“,”
附近触发语法错误THEN resultTable.Country, resultTable.City, resultTable.Street, resultTable.PostalCode END,
如果我在第一个“,”之后删除列,一切都没问题,但我正在尝试按地址排序,该地址由4列组成。可能我在这里看不到一些容易的东西。
PS:@sortBy
只是一个字符串参数。
答案 0 :(得分:3)
你可能想要
CASE WHEN @sortBy = 'Address'
THEN
isnull(resultTable.Country,'')
+ isnull( resultTable.City,'')
+ isnull( resultTable.Street,'')
+ isnull( resultTable.PostalCode,'')
end
答案 1 :(得分:3)
尝试这样的事情:
ORDER BY
CASE WHEN @sortBy = 'OrderNumber'
THEN resultTable.OrderNumber END,
CASE WHEN @sortBy = 'ProjectId'
THEN resultTable.ProjectId END,
CASE WHEN @sortBy = 'Address'
THEN resultTable.Country END,
CASE WHEN @sortBy = 'Address'
THEN resultTable.City END,
CASE WHEN @sortBy = 'Address'
THEN resultTable.Street END,
CASE WHEN @sortBy = 'Address'
THEN resultTable.PostalCode END,
CASE WHEN @sortBy = 'CreatedDate'
THEN resultTable.CreatedDate END
答案 2 :(得分:0)
假设@sortBy只能有那4个值
CASE WHEN @sortBy = 'OrderNumber' THEN resultTable.OrderNumber END,
CASE WHEN @sortBy = 'ProjectId' THEN resultTable.ProjectId END,
CASE WHEN @sortBy = 'CreatedDate' THEN resultTable.CreatedDate END,
CASE WHEN @sortBy = 'Address' THEN resultTable.Country END,
resultTable.City, resultTable.Street, resultTable.PostalCode