很強大的sqlserver分頁預存程序

來源:互聯網
上載者:User
代碼

-- ================================================
-- 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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.