我正在使用 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
只是一个字符串参数。