使用临时表对存储过程中的日期时间行进行排序

时间:2016-03-23 06:49:26

标签: sql-server sorting datetime stored-procedures temp-tables

我有一个存储过程,它从多个表中选择数据(使用连接),并根据提供的列以排序顺序(asc或desc)返回它。

为了根据输入生成排序结果,我使用临时表。

我的问题是,当提供的列是datetime时,则排序不起作用。 对于其他列,该过程按预期工作。

这是我使用的代码。

ALTER PROCEDURE [dbo].[usp_GetEULADetails]
(
    @OrgKey INT

    ,@FilterParams FilterTypes READONLY,
    /*– Sorting Parameters */
    @SortColumn nvarchar(20) = 'Status' -- BranchCode
    ,@SortOrder nvarchar(4) = 'Asc'

    /*– Pagination Parameters */
    ,@PageNumber int = 0
    ,@PageSize int = 0


)

AS

BEGIN

SET NOCOUNT ON;

 DECLARE @SortCol nvarchar(20),
      @FirstRec int,
      @LastRec int,
      @TotalRows int,
      @GlobalSearch nvarchar(100),
      @EULAVersionNumber nvarchar(100)

DECLARE @StatusKey INT =(SELECT dbo.GetStatusKey('ACTIVE'))
DECLARE @RowCountSelected INT

SELECT
    @EULAVersionNumber = LTRIM(RTRIM(Value))
FROM @FilterParams
WHERE [FieldName] = 'EULAVersions'

SELECT
    @GlobalSearch = LTRIM(RTRIM(Value))
FROM @FilterParams
WHERE [IsGlobal] = 1

SET @SortCol = LTRIM(RTRIM(@SortColumn))
SET @FirstRec = (@PageNumber - 1) * @PageSize
SET @LastRec = (@PageNumber * @PageSize + 1)

CREATE TABLE #ResultTempTable (
ROWNUM int
,ORGKEY int
,EULAVersionKey int
,EULAVersionNumber varchar(25)
,EULAVersionUpdateDate datetime
,STATUSKEY tinyint
,DocumentFileName varchar(100)
,DocumentGUID varchar(100)
,DocumentTypeName varchar(50)
,DocumentDescription varchar(128)
,ContactKey int
,ContactFirstName varchar(50)
,ContactMiddleName varchar(50)
,ContactLastName varchar(50)
,StatusName varchar(25)
)
insert into #ResultTempTable (
        ROWNUM
        ,ORGKEY 
        ,EULAVersionKey 
        ,EULAVersionNumber 
        ,EULAVersionUpdateDate 
        ,STATUSKEY 
        ,DocumentFileName 
        ,DocumentGUID 
        ,DocumentTypeName 
        ,DocumentDescription
        ,ContactKey 
        ,ContactFirstName 
        ,ContactMiddleName
        ,ContactLastName
        ,StatusName 
)
(
    SELECT 
        ROW_NUMBER() OVER (ORDER BY
        CASE
        WHEN (@SortCol = 'EULAVersionNumber' AND
            @SortOrder = 'ASC') THEN V.EULAVersionNumber
        END ASC,
        CASE
        WHEN (@SortCol = 'EULAVersionNumber' AND
            @SortOrder = 'DESC') THEN V.EULAVersionNumber
        END DESC,
        CASE
        WHEN (@SortCol = 'DocumentFileName' AND
            @SortOrder = 'ASC') THEN V.DocumentFileName
        END ASC,
        CASE
        WHEN (@SortCol = 'DocumentFileName' AND
            @SortOrder = 'DESC') THEN V.DocumentFileName
        END DESC,
        CASE
            WHEN (@SortCol = 'EULAVersionUpdateDate' AND
              @SortOrder = 'ASC') THEN convert(date, V.EULAVersionUpdateDate,103)
          END ASC,
        CASE
            WHEN (@SortCol = 'EULAVersionUpdateDate' AND
              @SortOrder = 'DESC') THEN convert(date, V.EULAVersionUpdateDate,103)
          END DESC,

        CASE
        WHEN (@SortCol = 'ContactFirstName' AND
            @SortOrder = 'ASC') THEN V.ContactFirstName
        END ASC,
        CASE
        WHEN (@SortCol = 'ContactFirstName' AND
            @SortOrder = 'DESC') THEN V.ContactFirstName
        END DESC,

        CASE
        WHEN (@SortCol = 'StatusKey' AND
            @SortOrder = 'ASC') THEN V.StatusKey
        END ASC,
        CASE
        WHEN (@SortCol = 'StatusKey' AND
            @SortOrder = 'DESC') THEN V.StatusKey
        END DESC

  ) AS ROWNUM
    ,[ORGKEY]
    ,[EULAVersionKey]
    ,[EULAVersionNumber]
    ,[EULAVersionUpdateDate]
    ,[STATUSKEY]
    ,[DocumentFileName]
    ,[DocumentGUID]
    ,[DocumentTypeName]
    ,[DocumentDescription]
    ,[ContactKey]
    ,[ContactFirstName]
    ,[ContactMiddleName]
    ,[ContactLastName]
    ,[StatusName]

FROM (
        SELECT
            EUV.[ORGKEY]
            ,EUV.[EULAVersionKey]
            ,EUV.[EULAVersionNumber]
            ,EUV.[EULAVersionUpdateDate]
            ,EUV.[STATUSKEY]
            ,DOC.[DocumentFileName]
            ,DOC.[DocumentGUID]
            ,DOC.[DocumentDescription]
            ,DOCTYP.[DocumentTypeName]
            ,CN.[ContactKey]
            ,CN.[ContactFirstName]
            ,CN.[ContactMiddleName]
            ,CN.[ContactLastName]
            ,ST.[StatusName]

        FROM [dbo].[EULAVersions] EUV
            JOIN [dbo].[Documents] DOC
                ON DOC.[DocumentKey]  =EUV.[DocumentKey]
            JOIN[dbo].[DocumentTypes] DOCTYP
                ON DOCTYP.[DocumentTypeKey]=EUV.[DocumentTypeKey]
            JOIN [dbo].[UserContacts] UC
                ON UC.[UserKey]=EUv.[EULAVersionUpdatedBy]
            JOIN [dbo].[Contacts] CN
                ON CN.[ContactKey]=UC.[ContactKey]
            JOIN [dbo].[StatusTypes] ST
                ON ST.[StatusKey]=EUV.[StatusKey]

        WHERE 
            EUV.[ORGKEY]=@OrgKey --AND EUV.[StatusKey]=@StatusKey
            AND EUV.[EULAVersionNumber] LIKE '%' + ISNULL(@EULAVersionNumber, EULAVersionNumber) + '%'
            AND EUV.[EULAVersionNumber] LIKE '%' + ISNULL(@GlobalSearch, EULAVersionNumber) + '%') AS V

    )
IF (@PageNumber <> 0)
    BEGIN
        SELECT
            @TotalRows = COUNT(1)
        FROM #ResultTempTable AS CPC
        WHERE ROWNUM BETWEEN @FirstRec AND CASE
            WHEN @PageNumber = 0 THEN @TotalRows
            ELSE @LastRec
        END

        SELECT 
             [ORGKEY]
            ,[EULAVersionKey]
            ,[EULAVersionNumber]
            ,[EULAVersionUpdateDate]
            ,[STATUSKEY]
            ,[DocumentFileName]
            ,[DocumentGUID]
            ,[DocumentDescription]
            ,[DocumentTypeName]
            ,[ContactFirstName]
            ,[ContactMiddleName]
            ,[ContactLastName]
            ,[StatusName]

        FROM #ResultTempTable AS CPC

        WHERE ROWNUM BETWEEN @FirstRec AND CASE
          WHEN @PageNumber = 0 THEN @TotalRows
          ELSE @LastRec
        END
        ORDER BY ROWNUM ASC
      END
      ELSE
      BEGIN
        SELECT
          @TotalRows = COUNT(1)
        FROM #ResultTempTable AS CPC


        SELECT 
             [ORGKEY]
            ,[EULAVersionKey]
            ,[EULAVersionNumber]
            ,[EULAVersionUpdateDate]
            ,[STATUSKEY]
            ,[DocumentFileName]
            ,[DocumentGUID]
            ,[DocumentDescription]
            ,[DocumentTypeName]
            ,[ContactFirstName]
            ,[ContactMiddleName]
            ,[ContactLastName]
            ,[StatusName]

        FROM #ResultTempTable AS CPC

        ORDER BY ROWNUM ASC
END

 SELECT @TotalRows AS TotalCount
   IF OBJECT_ID('tempdb..#ResultTempTable') IS NOT NULL
    DROP TABLE #ResultTempTable
END

- FilterTypes详细信息

CREATE TYPE [dbo].[FilterTypes] AS TABLE(
[FieldName] [varchar](100) NOT NULL,
[Value] [varchar](800) NOT NULL,
[IsGlobal] [bit] NULL DEFAULT ((0))
)

Sample Exec

declare @FilterParams AS FilterTypes;
insert into @FilterParams values('EULAVersions','',1)

exec [usp_GetEULADetails] 2,@FilterParams,'EULAVersionUpdateDate','desc',0,0

获得的结果

Unsorted Output for datetime

预期结果

declare @FilterParams AS FilterTypes;
insert into @FilterParams values('EULAVersions','',1)

exec [usp_GetEULADetails] 2,@FilterParams,'ContactFirstName','desc',0,0

Sorted Output as expected

我面临的问题是当数据类型是datetime时如何正确排序数据?

先谢谢你的帮助....

1 个答案:

答案 0 :(得分:1)

您正在转换&#39; V.EULAVersionUpdateDate&#39;到日期DataType(...THEN convert(date, V.EULAVersionUpdateDate,103)),使用DATETIME数据类型进行转换

THEN convert(DateTime, V.EULAVersionUpdateDate,103)