分页存储过程

CREATE PROCEDURE  sp_fenye

@Page int,
@Size int

AS

DECLARE @Start int, @End int

BEGIN TRANSACTION GetDataSet

SET @Start = (((@Page - 1) * @Size) + 1)
IF @@ERROR <> 0
 GOTO ErrorHandler

SET @End = (@Start + @Size - 1)
IF @@ERROR <> 0
 GOTO ErrorHandler

CREATE TABLE #TemporaryTable
(
 Row int IDENTITY(1,1) PRIMARY KEY,
 CustomerID nchar(5),
        OrderDate datetime,
        ShipName  nvarchar(40)
)
IF @@ERROR <> 0
 GOTO ErrorHandler

INSERT INTO #TemporaryTable
SELECT CustomerID,OrderDate,ShipName FROM Orders

IF @@ERROR <> 0
 GOTO ErrorHandler

SELECT CustomerID,OrderDate,ShipName
FROM #TemporaryTable
WHERE (Row >= @Start) AND (Row <= @End)
IF @@ERROR <> 0
 GOTO ErrorHandler

DROP TABLE #TemporaryTable

COMMIT TRANSACTION GetDataSet
RETURN 0

ErrorHandler:
ROLLBACK TRANSACTION GetDataSet
RETURN @@ERROR

你可能感兴趣的:(存储过程)