[英]How to achieve Custom Paging in SQL Pivot Query
I have a stored procedure accepts date and month as parameter that results monthly attendance sheet. 我有一个存储过程接受日期和月份作为产生每月出勤表的参数。 In order to achieve Paging function, a parameter called 'PageNo' which contain page index.
为了实现分页功能,一个名为“ PageNo”的参数包含页面索引。 I haveto show results based on page No.
我必须根据页面编号显示结果。
Stored Procedure 储存程序
ALTER procedure [dbo].[sps_AttendanceShowModified] @mon int, @year
int,@pageNo int As begin DECLARE @cols AS NVARCHAR(MAX)=''; DECLARE
@query AS NVARCHAR(MAX)='';
set @query = 'SELECT * from (
select e.Name,a.WorkHours,DAY(a.AttendaceDate) AS d1 from Attendace a,employee e where e.EmpID=a.EmpID and
MONTH(a.AttendaceDate)= ' + CONVERT(VARCHAR(12), @mon)+ ' AND
YEAR(a.AttendaceDate)= ' + CONVERT(VARCHAR(12), @year)+ ' ) src pivot
(
max(WorkHours) for d1 in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30])
) piv'
execute(@query) end
For this you could amend your code to use OFFSET like this: 为此,您可以修改代码以使用OFFSET,如下所示:
DECLARE @pageSize INT;
DECLARE @pageNumber INT;
SET @pageSize = 1000;
SET @pageNumber = 1;
SELECT my_columns_here
FROM dbo.my_table_here
ORDER BY my_ordered_column
OFFSET @pageSize * (@pageNumber - 1) ROWS
FETCH NEXT @pageSize ROWS ONLY;
On a separate note, I noticed you are using a classic JOIN: 另外,我注意到您使用的是经典的JOIN:
SELECT blabla
FROM Attendace a, employee e
WHERE e.EmpID = a.EmpID
Most developers and dba's will use ANSI-92 as a standard these days: 如今,大多数开发人员和dba都会使用ANSI-92作为标准:
SELECT blabla
FROM Attendace a
INNER JOIN employee e ON a.EmpID = e.EmpID
This way you tend to avoid accidental CROSS joins and unnecessary ambiguity by separating relation from filter logic. 这样,通过将关系与过滤器逻辑分开,可以避免意外的CROSS连接和不必要的歧义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.