我正在尝试通过使用以下查询(例如将参数传递给存储过程以及排序方向和按列排序作为输入参数)来按列排序并在列中使用空值
ALTER PROCEDURE [dbo].[tp_SelectTransactionHistorySearch]
(
@OffSetRowNo INT,
@FetchRowNo INT,
@StatusSelection NVARCHAR(MAX),
@IsReviewed Bit,
@ProjectCaseNumber VARCHAR(MAX),
@CostPageNumber VARCHAR(MAX),
@TransactionTypeChange VARCHAR(MAX),
@DescriptionChange VARCHAR(MAX),
@TrasactionCreateOnBeginDate DATE,
@TransactionCreatedOnEndDate DATE,
@TransactionUpdatedOnBeginDate DATE,
@TransactionUpdateOnEndDate DATE,
@ItemID VARCHAR(MAX),
@SortOrder VARCHAR(MAX),
@SortByColumnName VARCHAR(MAX)
)
AS
SELECT
TransactionID,IsReviewed,ItemID,CostPageNumber,Comments,
CreatedBy,CreatedDateTime,UpdatedBy,UpdatedDateTime,
TransactionDescription,TransactionTypeID,PROJCASE,
TransactionStatusID, TransactionStatusTypeName,
TransactionStatusTypeDescription,
TransactionTypeName, TransactionTypeDescription,
COUNT(*) OVER () as TotalCount
FROM
( //select
// union
// select
// union
//like select statement from union results
) ResultSet
//here i am doing sorting
ORDER BY
CASE @SortOrder
WHEN 'ASC' THEN
CASE @SortByColumnName
WHEN 'TransactionID' THEN CONVERT(VARCHAR(MAX), ResultSet.TransactionID)
WHEN 'ItemID' THEN CONVERT(VARCHAR(MAX), ResultSet.ItemID)
WHEN 'CostPageNumber' THEN ResultSet.CostPageNumber
WHEN 'Comments' THEN ResultSet.Comments
WHEN 'CreatedBy' THEN ResultSet.CreatedBy
WHEN 'CreatedDateTime' THEN CONVERT(VARCHAR(MAX), ResultSet.CreatedDateTime)
WHEN 'UpdatedBy' THEN ResultSet.UpdatedBy
WHEN 'UpdatedDateTime' THEN CONVERT(VARCHAR(MAX), ResultSet.UpdatedDateTime)
WHEN 'TransactionDescription' THEN ResultSet.TransactionDescription
WHEN 'TransactionTypeName' THEN ResultSet.TransactionTypeName
WHEN 'PROJCASE' THEN ResultSet.PROJCASE
WHEN 'TransactionStatusTypeName' THEN ResultSet.TransactionStatusTypeName
WHEN 'TransactionStatusTypeDescription' THEN ResultSet.TransactionStatusTypeDescription
END
END
ASC,
CASE @SortOrder
WHEN 'DESC' THEN
CASE @SortByColumnName
WHEN 'TransactionID' THEN CONVERT(VARCHAR(MAX), ResultSet.TransactionID)
WHEN 'ItemID' THEN CONVERT(VARCHAR(MAX), ResultSet.ItemID)
WHEN 'CostPageNumber' THEN ResultSet.CostPageNumber
WHEN 'Comments' THEN ResultSet.Comments
WHEN 'CreatedBy' THEN ResultSet.CreatedBy
WHEN 'CreatedDateTime' THEN CONVERT(VARCHAR(MAX), ResultSet.CreatedDateTime)
WHEN 'UpdatedBy' THEN ResultSet.UpdatedBy
WHEN 'UpdatedDateTime' THEN CONVERT(VARCHAR(MAX), ResultSet.UpdatedDateTime)
WHEN 'TransactionDescription' THEN ResultSet.TransactionDescription
WHEN 'TransactionTypeName' THEN ResultSet.TransactionTypeName
WHEN 'PROJCASE' THEN ResultSet.PROJCASE
WHEN 'TransactionStatusTypeName' THEN ResultSet.TransactionStatusTypeName
WHEN 'TransactionStatusTypeDescription' THEN ResultSet.TransactionStatusTypeDescription
END
END
OFFSET ( @OffSetRowNo-1 ) * @FetchRowNo ROWS
FETCH NEXT @FetchRowNo ROWS ONLY
在这里不使用任何 orderBy(排序方法)我得到two rows
的结果即使它们是列中的空值但使用排序(即)OrderBy 我只能得到一行作为结果,我不知道如何处理空值使用 order by 时,但我尝试了以下语句
WHEN ColumnB IS NULL THEN 1 ELSE 0 END ASC,
但是当应用这个时,我遇到语法错误....在这种情况下订购时,请提出任何想法如何处理空值.. 我使用的是 sql server 2012 版
提前谢谢了 ....