如何在 这段储存过程中加分页 create PROCEDURE [dbo].[Z_SelfHelpOrderingQuery] @CorpId varchar(20), @EnCode varchar(20), @MealTypeId varchar(20), @AreaId varchar(20), @CheackMark varchar(20), @MenuName varchar(20), @Start varchar(20), @End varchar(20), @MenuCode varchar(20), @BilltypeId varchar(20) AS begin if (@CheackMark = 1) BEGIN select A.MenuId as id, CONVERT(varchar(10), A.MenuDate, 120) as MenuDate, CONVERT(varchar(10), A.SalesDate, 120) as SalesDate, A.MenuCode, A.MealType, A.MenuName, A.CheackMark, A.ShortName, A.BilltypeId, A.CreateUserId, A.MenuCycleId, A.EnCode, A.MealTypeId,B.AreaId,A.CreateUserName,A.ModifyUserName from YE_School_Menu A WITH(NOLOCK) left join YE_Client_Customer B WITH(NOLOCK) on A.CorpId=B.CorpId And B.EnabledMark=1 and A.EnCode=B.EnCode where A.CorpId=@CorpId and A.MenuCode like @MenuCode and A.CheackMark=@CheackMark and A.BilltypeId=@BilltypeId and A.MealTypeId like @MealTypeId and A.MenuName like @MenuName and A.MenuDate >= @Start and A.MenuDate<=@End and B.AreaId like @AreaId and A.EnCode like @EnCode group by A.MenuId,A.MenuDate,A.MenuCode,A.MealType,A.MenuName,A.CheackMark,A.SalesDate,A.ShortName,A.CreateUserId,A.MenuCycleId,A.EnCode,A.MealTypeId,A.BilltypeId ,B.AreaId,A.CreateUserName,A.ModifyUserName order by A.MenuDate,A.ShortName,A.MealTypeId,A.MenuName,A.SalesDate END select A.MenuId as id, CONVERT(varchar(10), A.MenuDate, 120) as MenuDate, CONVERT(varchar(10), A.SalesDate, 120) as SalesDate, A.MenuCode, A.MealType, A.MenuName, A.CheackMark, A.ShortName, A.BilltypeId, A.CreateUserId, A.MenuCycleId, A.EnCode, A.MealTypeId,B.AreaId,A.CreateUserName,A.ModifyUserName from YE_School_Menu A WITH(NOLOCK) left join YE_Client_Customer B WITH(NOLOCK) on A.CorpId=B.CorpId And B.EnabledMark=1 and A.EnCode=B.EnCode where A.CorpId=@CorpId and A.MenuCode like @MenuCode and A.BilltypeId=@BilltypeId and A.MealTypeId like @MealTypeId and A.MenuName like @MenuName and A.MenuDate >= @Start and A.MenuDate<=@End and B.AreaId like @AreaId and A.EnCode like @EnCode group by A.MenuId,A.MenuDate,A.MenuCode,A.MealType,A.MenuName,A.CheackMark,A.SalesDate,A.ShortName,A.CreateUserId,A.MenuCycleId,A.EnCode,A.MealTypeId,A.BilltypeId ,B.AreaId,A.CreateUserName,A.ModifyUserName order by A.MenuDate,A.ShortName,A.MealTypeId,A.MenuName,A.SalesDate
时间: 2024-03-11 20:51:48 浏览: 77
关于SQL数据库 msdb.dbo.sp_send_dbmail 函数发送邮件的场景分析
以下是在存储过程中添加分页的示例代码:
```
CREATE PROCEDURE [dbo].[Z_SelfHelpOrderingQuery]
@CorpId varchar(20),
@EnCode varchar(20),
@MealTypeId varchar(20),
@AreaId varchar(20),
@CheackMark varchar(20),
@MenuName varchar(20),
@Start varchar(20),
@End varchar(20),
@MenuCode varchar(20),
@BilltypeId varchar(20),
@PageNumber INT,
@PageSize INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Offset INT = (@PageNumber - 1) * @PageSize;
-- First query for getting total records
DECLARE @TotalRecords INT;
SELECT @TotalRecords = COUNT(*) FROM YE_School_Menu WITH(NOLOCK)
LEFT JOIN YE_Client_Customer WITH(NOLOCK) ON YE_School_Menu.CorpId=YE_Client_Customer.CorpId AND YE_School_Menu.EnCode=YE_Client_Customer.EnCode
WHERE YE_School_Menu.CorpId=@CorpId AND YE_School_Menu.MenuCode LIKE @MenuCode AND YE_School_Menu.BilltypeId=@BilltypeId AND YE_School_Menu.MealTypeId LIKE @MealTypeId AND YE_School_Menu.MenuName LIKE @MenuName AND YE_School_Menu.MenuDate >= @Start AND YE_School_Menu.MenuDate <= @End AND YE_Client_Customer.AreaId LIKE @AreaId AND YE_School_Menu.EnCode LIKE @EnCode AND YE_School_Menu.CheackMark=@CheackMark AND YE_Client_Customer.EnabledMark=1;
-- Second query for getting paged data
SELECT *
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY YE_School_Menu.MenuDate, YE_School_Menu.ShortName, YE_School_Menu.MealTypeId, YE_School_Menu.MenuName, YE_School_Menu.SalesDate) AS RowNum,
YE_School_Menu.MenuId AS id,
CONVERT(varchar(10), YE_School_Menu.MenuDate, 120) AS MenuDate,
CONVERT(varchar(10), YE_School_Menu.SalesDate, 120) AS SalesDate,
YE_School_Menu.MenuCode,
YE_School_Menu.MealType,
YE_School_Menu.MenuName,
YE_School_Menu.CheackMark,
YE_School_Menu.ShortName,
YE_School_Menu.BilltypeId,
YE_School_Menu.CreateUserId,
YE_School_Menu.MenuCycleId,
YE_School_Menu.EnCode,
YE_School_Menu.MealTypeId,
YE_Client_Customer.AreaId,
YE_School_Menu.CreateUserName,
YE_School_Menu.ModifyUserName
FROM YE_School_Menu WITH(NOLOCK)
LEFT JOIN YE_Client_Customer WITH(NOLOCK) ON YE_School_Menu.CorpId=YE_Client_Customer.CorpId AND YE_School_Menu.EnCode=YE_Client_Customer.EnCode
WHERE YE_School_Menu.CorpId=@CorpId AND YE_School_Menu.MenuCode LIKE @MenuCode AND YE_School_Menu.BilltypeId=@BilltypeId AND YE_School_Menu.MealTypeId LIKE @MealTypeId AND YE_School_Menu.MenuName LIKE @MenuName AND YE_School_Menu.MenuDate >= @Start AND YE_School_Menu.MenuDate <= @End AND YE_Client_Customer.AreaId LIKE @AreaId AND YE_School_Menu.EnCode LIKE @EnCode AND YE_School_Menu.CheackMark=@CheackMark AND YE_Client_Customer.EnabledMark=1
) AS PaginatedData
WHERE PaginatedData.RowNum > @Offset AND PaginatedData.RowNum <= (@Offset + @PageSize)
ORDER BY PaginatedData.MenuDate, PaginatedData.ShortName, PaginatedData.MealTypeId, PaginatedData.MenuName, PaginatedData.SalesDate;
-- Third query for getting total pages
DECLARE @TotalPages INT = CEILING(CAST(@TotalRecords AS FLOAT) / @PageSize);
SELECT @TotalPages AS TotalPages;
END
```
在调用存储过程时,传入页码和每页记录数参数即可获取指定页码的数据。例如:
```
EXEC Z_SelfHelpOrderingQuery @CorpId='1', @EnCode='', @MealTypeId='', @AreaId='', @CheackMark='1', @MenuName='', @Start='2022-01-01', @End='2022-12-31', @MenuCode='', @BilltypeId='1', @PageNumber=1, @PageSize=10;
```
以上示例中,@PageNumber=1 表示获取第一页的数据,@PageSize=10 表示每页显示 10 条记录。
阅读全文