CREATE PROCEDURE dt_DXS_STAFF_ACTIVE @STAFFSTATUS INT, @STAFFNUM INT, @STARTNO INT, @@TOTALCOUNT INT OUTPUT, @@REALNUM INT OUTPUT AS DECLARE @PRERECCOUNT VARCHAR(10) DECLARE @CURRECCOUNT VARCHAR(10) SELECT @@TOTALCOUNT=COUNT(*) FROM Table1 IF @@TOTALCOUNT>(@STARTNO+1)*@STAFFNUM SET @@REALNUM=@STAFFNUM ELSE SET @@REALNUM=@@TOTALCOUNT-@STARTNO*@STAFFNUM SET @CURRECCOUNT=CAST( @STARTNO * @STAFFNUM + @@REALNUM AS VARCHAR( 10 ) ) IF @STARTNO = 0 EXEC( 'SELECT TOP ' + @CURRECCOUNT + ' ID,NAME,Sex,Address,Salary FROM Table1 WHERE STATUS='+@STAFFSTATUS+' ORDER BY ID ASC' ) ELSE BEGIN SET @PRERECCOUNT= CAST( @STARTNO * @STAFFNUM AS VARCHAR( 10 ) ) EXEC( 'SELECT TOP ' + @CURRECCOUNT + ' ID,NAME,Sex,Address,Salary FROM Table1 WHERE ID NOT IN ' + '(SELECT TOP ' + @PRERECCOUNT + 'ID FROM Table1 WHERE STATUS='+@STAFFSTATUS+' ORDER BY ID ASC) ' + 'ORDER BY ID ASC' ) END GO