Сортировка и фильтрация подкачки T-SQL
Я уже несколько часов работаю над хранимой процедурой T-SQL, которая позволит мне получить постраничный набор статей, отсортированных в ASC илиПорядок DESC на основе указанного столбца.
Сейчас я работаю над получением хранимой процедуры для фильтрации по первому символу поля 'Title' и добавил строки:
@StartAlpha nvarchar(1) = null
и
WHERE ((@StartAlpha IS NULL) OR (Title Like @StartAlpha + '%'))
см. ниже.
Хранимая процедура больше не возвращает никаких результатов.И я не знаю, почему.
Кто-нибудь может помочь, пожалуйста?
С уважением
Уолтер
USE [ABC]
GO
/****** Object: StoredProcedure [dbo].[Get_MyArticles_Paged] Script Date: 08/07/2011 20:41:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Get_MyArticles_Paged]
/*Paging Total For Output*/
@Row_Count BIGINT OUT,
/*Paging Inputs*/
@Page_Size INT = 10,
@Page_Number INT = 1,
@Sort_Column VARCHAR(100), /* ('articleid','createdate','title','subject') */
@Sort_Direction VARCHAR(4), /* ('ASC','DESC') */
@StartAlpha nvarchar(1) = null
AS
BEGIN
print @StartAlpha
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
/*========================================================================
Declare local variables
========================================================================*/
DECLARE @FirstRecord int
DECLARE @LastRecord int
-- create a temporary space for paged result set
DECLARE @PagedResults AS TABLE (
[ArticleID] INT,
[CreateDate] SMALLDATETIME,
[Title] VARCHAR(200),
[Subject] VARCHAR(500),
[Row_Number] BIGINT,
[Row_Count] BIGINT
);
/*========================
Normalize Paging Parameters
==========================*/
--Fix invalid input for Page Size
SET @Page_Size = CASE
WHEN @Page_Size IS NULL THEN 10
WHEN @Page_Size < 1 THEN 10
ELSE @Page_Size
END;
--Fix invalid input for Page Number
SET @Page_Number = CASE
WHEN @Page_Number IS NULL THEN 1
WHEN @Page_Number < 1 THEN 1
ELSE @Page_Number
END;
--starting record to use.
SET @FirstRecord = ((@Page_Number - 1) * @Page_Size) + 1
--last record to use.
SET @LastRecord = @FirstRecord + @Page_Size - 1
--ensure sort column is valid in the list
SET @Sort_Column = CASE
WHEN LOWER(@Sort_Column) IN ('articleid','createdate','title','subject')
THEN LOWER(@Sort_Column)
ELSE
'title' --default
END
--ensure sort direction is ASC or DESC
SET @Sort_Direction = CASE
WHEN LEFT(UPPER(COALESCE(@Sort_Direction, '')) + ' ', 4) = 'DESC'
THEN 'DESC' --explicit descending
WHEN @Sort_Column = 'created' AND LEFT(UPPER(COALESCE(@Sort_Direction,'')) + ' ', 3) <> 'ASC' THEN
'DESC' --default for created date
ELSE 'ASC' --default otherwise
END;
/*============
Prepare Results
==============*/
WITH [MyTempArea] AS (
SELECT TOP (@LastRecord)
[ArticleID],
[CreateDate],
[Title],
[Subject],
ROW_NUMBER() OVER (
ORDER BY
CASE WHEN(@Sort_Direction = 'ASC') THEN CASE WHEN @Sort_Column='articleid' THEN [articleid] END END ASC,
CASE WHEN(@Sort_Direction = 'ASC') THEN CASE WHEN @Sort_Column='createdate' THEN [createdate] END END ASC,
CASE WHEN(@Sort_Direction = 'ASC') THEN CASE WHEN @Sort_Column='title' THEN [title] END END ASC,
CASE WHEN(@Sort_Direction = 'ASC') THEN CASE WHEN @Sort_Column='subject' THEN [subject] END END ASC,
CASE WHEN(@Sort_Direction = 'DESC') THEN CASE WHEN @Sort_Column='articleid' THEN [articleid] END END DESC,
CASE WHEN(@Sort_Direction = 'DESC') THEN CASE WHEN @Sort_Column='createdate' THEN [createdate] END END DESC,
CASE WHEN(@Sort_Direction = 'DESC') THEN CASE WHEN @Sort_Column='title' THEN [title] END END DESC,
CASE WHEN(@Sort_Direction = 'DESC') THEN CASE WHEN @Sort_Column='subject' THEN [subject] END END DESC
) AS [Row_Number],
COUNT(*) OVER () AS [Row_Count]
FROM Articles
WHERE ((@StartAlpha IS NULL) OR (Title Like @StartAlpha + '%'))
)
INSERT INTO @PagedResults
SELECT * FROM [MyTempArea] WHERE [Row_Number] >= @FirstRecord;
/*===========
Return Results
=============*/
-- @Row_Count output param
SELECT @Row_Count = COALESCE(MAX(Row_Count), 0) FROM @PagedResults;
-- Paged results set to return
SELECT [ArticleID],[CreateDate],[Title],[Subject]
FROM @PagedResults
ORDER BY [Row_Number];
END