Powerful SQL Server paging stored procedures

Source: Internet
Author: User
Code

 -- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE ContractRecord_GetAll_Page
-- Add the parameters for the stored procedure here
@filterExpression NVARCHAR(2000),
@sortExpression NVARCHAR(100),
@rowIndex INT = 0,
@pageSize INT = 24,
@TotalRecords INT OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
CREATE TABLE #GoodsKind (RowNumber INT, ContractID uniqueidentifier)
IF ((@sortExpression IS NULL) OR (LEN(@sortExpression) = 0))
BEGIN
SET @sortExpression = 'ContractCode asc'
END

DECLARE @SQLString NVARCHAR(MAX)
SET @SQLString = N'
INSERT INTO #GoodsKind
SELECT ROW_NUMBER() OVER (ORDER BY ' + @sortExpression + ') AS RowNumber, ContractID
FROM vw_ContractRecord'
IF ((@filterExpression IS NOT NULL) AND (LEN(@filterExpression) > 0))
BEGIN
SET @SQLString = @SQLString + ' WHERE ' + @filterExpression
END

EXECUTE sp_executesql @SQLString

SELECT @TotalRecords = COUNT(ContractID)
FROM #GoodsKind
-- Insert statements for procedure here
SELECT p.ContractID,EntCode,ContractCode,GoodsLimitDepartment,Supplier,Merchandiser,ContractTime,
Remark,RealAmount,TaxAmount,RealAndTaxAmount,OperatorName,InputDate
FROM #GoodsKind as p INNER JOIN vw_ContractRecord as C on p.ContractID = C.ContractID
WHERE p.RowNumber BETWEEN @rowIndex + 1 AND @rowIndex + @PageSize ORDER BY p.RowNumber
END
GO

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.