我有一个存储过程接受日期和月份作为产生每月出勤表的参数。为了实现分页功能,一个名为“ PageNo”的参数包含页面索引。我必须根据页面编号显示结果。
存储过程
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
答案 0 :(得分:0)
为此,您可以修改代码以使用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;
另外,我注意到您正在使用经典的JOIN:
SELECT blabla
FROM Attendace a, employee e
WHERE e.EmpID = a.EmpID
如今,大多数开发人员和dba都将ANSI-92作为标准使用:
SELECT blabla
FROM Attendace a
INNER JOIN employee e ON a.EmpID = e.EmpID
这样,通过将关系与过滤器逻辑分离,您倾向于避免意外的CROSS连接和不必要的歧义。