程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> sqlserver 支撐定位以後頁,自界說排序的分頁SQL(謝絕靜態SQL)

sqlserver 支撐定位以後頁,自界說排序的分頁SQL(謝絕靜態SQL)

編輯:MSSQL

sqlserver 支撐定位以後頁,自界說排序的分頁SQL(謝絕靜態SQL)。本站提示廣大學習愛好者:(sqlserver 支撐定位以後頁,自界說排序的分頁SQL(謝絕靜態SQL))文章只能為提供參考,不一定能成為您想要的結果。以下是sqlserver 支撐定位以後頁,自界說排序的分頁SQL(謝絕靜態SQL)正文


1,場景:依據先生編號查詢,前往該先生地點班級的一切先生。支撐分頁、自界說排序及成果集主動定位到查詢前提的先生編號地點頁。

CREATE PROC [dbo].[Sp_testpagerandsorting] (@GroupID INT,
@CurrentId INT,
@TimeFrom DATETIME,
@TimeTo DATETIME,
@OrderBy CHAR(50),
@PageSize INT,
@CurrentPage INT)
AS
SET nocount ON
BEGIN
DECLARE @StartNumber INT,
@EndNumber INT,
@CurrentIdRowNumber INT,
@RecordCount INT,
@EndPageIndex INT
DECLARE @RowNumberTable TABLE (
rownumber INT IDENTITY (1, 1),
id INT )
--step 1: Build sort id list -------------------------------------------------------
INSERT INTO @RowNumberTable
(id)
SELECT sm.id AS id
FROM dbo.test sm WITH (nolock)
WHERE indate BETWEEN Coalesce(@TimeFrom, indate) AND
Coalesce(@TimeTo, indate)
AND sm.groupid = @GroupID
ORDER BY CASE
WHEN @OrderBy = 'InDate desc' THEN ( Row_number() OVER (ORDER BY indate DESC))
WHEN @OrderBy = 'InDate asc' THEN (Row_number() OVER (ORDER BY indate ASC))
WHEN @OrderBy = 'Id asc' THEN (Row_number() OVER (ORDER BY sm.id ASC))
WHEN @OrderBy = 'Id desc' THEN (Row_number() OVER (ORDER BY sm.id DESC))
WHEN @OrderBy = 'Name asc' THEN (Row_number() OVER (ORDER BY sm.name ASC))
WHEN @OrderBy = 'Name desc' THEN (Row_number() OVER (ORDER BY sm.name DESC) )
END
--step 2: Reset page index with current id -----------------------------------------
IF @CurrentIdNumber > 0
BEGIN
SELECT TOP 1 @CurrentIdRowNumber = rownumber
FROM @RowNumberTable
WHERE id = @CurrentIdNumber
IF @CurrentIdRowNumber > 0
BEGIN
IF @CurrentPage = 0
BEGIN
SET @CurrentPage = Ceiling(CAST(@CurrentIdRowNumber AS DECIMAL) / CAST (@PageSize AS DECIMAL))
END
END
END
ELSE
BEGIN
IF @CurrentPage = 0
BEGIN
SET @CurrentPage = 1
END
END
--step 3: Set recordCount -----------------------------------------
SELECT @RecordCount = COUNT(1)
FROM @RowNumberTable
--step 4: Calc startNumber & endNumber -----------------------------------------
SELECT @StartNumber = @PageSize * ( @CurrentPage - 1 ),
@EndNumber = @PageSize * ( @CurrentPage - 1 ) + @pageSize,
@EndPageIndex = Ceiling(CAST(@RecordCount AS DECIMAL) / CAST(@PageSize AS DECIMAL))
IF @CurrentPage = @EndPageIndex
BEGIN
SET @EndNumber = @RecordCount
END
--step 5: Get sorted id of current page -----------------------------------------
;WITH a
AS (SELECT TOP (@EndNumber - @StartNumber) id,
rownumber
FROM (SELECT TOP (@EndNumber) id,
rownumber
FROM @RowNumberTable) AS b
ORDER BY rownumber DESC)
--step 6: Return current page idList -------------------------------------------------------
SELECT [ID],
[GroupID] [Name],
[Address]
FROM dbo.test sm WITH(nolock)
INNER JOIN a
ON a.id = sm.id
ORDER BY a.rownumber
-- step 7:return current page & record count ----------------------------------
SELECT @CurrentPage AS currentpage,
@RecordCount AS recordcount
END

2,簡略前提的,靜態where語句(關於Like查詢的靜態where,建議應用笨方法做)

CREATE PROC [dbo].[Getstudentlistbycondition] @Name NVARCHAR(20),
@Class INT
AS
SET nocount ON
BEGIN
BEGIN
SELECT [Name],
[class]
FROM [testtable]
WHERE [Class] = CASE
WHEN @Class > 0 THEN @Class ELSE [Class] END
AND [name] = CASE
WHEN @Name <> '' THEN @Name ELSE [Name] END
END
END
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved