1
複数のテーブルからデータを選択して(ジョインを使用して)、指定したカラムに基づいてソート順(ascまたはdesc)で返すストアドプロシージャを使用しています。tempテーブルのストアドプロシージャでdatetimeの行をソート
入力に基づいてソートされた結果を生成するために、私はTemp Tableを使用します。
私の問題は、提供された列が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][email protected] --AND EUV.[StatusKey][email protected]
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))
)
サンプルExecの
declare @FilterParams AS FilterTypes;
insert into @FilterParams values('EULAVersions','',1)
exec [usp_GetEULADetails] 2,@FilterParams,'EULAVersionUpdateDate','desc',0,0
declare @FilterParams AS FilterTypes;
insert into @FilterParams values('EULAVersions','',1)
exec [usp_GetEULADetails] 2,@FilterParams,'ContactFirstName','desc',0,0
私が直面している問題は、データ型が日時である場合に、適切にデータをソートする方法ですか?
事前に協力いただきありがとうございますが....
を変換するために、DATETIMEデータ型を使用uは私記述してくださいすることができ間違えた? – Bimzee
Row_Number()内でBY.WHEN(@SortCol = 'EULAVersionUpdateDate' AND @SortOrder = 'ASC')THEN convert(** date **、V.EULAVersionUpdateDate、103)を実行します。それはPlzを答えとしてこれを受け入れることを検討し、 –
OKおかげで男 をチェック.... – Bimzee