Code CreatePROCEDURE GeneratePageProc As Begin /* //为数据库中所有的用户数据表生成分页存储过程 */ --获取数据表名 declare@tablenamenvarchar(255) Declare@sqlnvarchar(4000) declare mycursor Cursor forselect name from sys.objects where type='U'and name<>'dtproperties' --获取字段名称、标识、字段序号、占用字节数、小数位数、允许空等 open mycursor fetchnextfrom mycursor into@tablename while(@@fetch_status=0) begin set@sql= ' -- Drop stored procedure if it already exists IF EXISTS ( SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE SPECIFIC_NAME = '''+@tablename+'_getpage'' ) DROP PROCEDURE '+@tablename+'_getpage GO Create PROCEDURE [dbo].['+@tablename+'_getpage] @pagesize int, @pageindex int, @orderby varchar(255) AS begin DECLARE @startRow int, @endRow int SET @startRow = (@pageindex - 1) * @pagesize + 1 SET @endRow = @startRow + @pagesize - 1 Declare @sql nvarchar(4000) SET @sql= '' SELECT * FROM ( SELECT ROW_NUMBER() OVER(ORDER BY ''+@orderby+'' ) AS ROWID, * FROM '+@tablename+') as sourse'' +'' where ROWID BETWEEN ''+ ltrim(rtrim(str(@startRow))) +'' AND ''+ ltrim(rtrim(str(@endRow))) print @sql exec sp_executesql @sql end go' exec sp_executesql @sql fetchnextfrom mycursor into@tablename end close mycursor deallocate mycursor end