標籤:style blog color ar sp div 2014 c on
下面是預存程序的建立,用的時候調用就行了
/****** Object: StoredProcedure [dbo].[sq_paging] Script Date: 09/10/2014 09:57:54 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE proc [dbo].[Yc_PageSelect]@tbName varchar(255), --表名@tbFields varchar(1000), --返回欄位@PageSize int, --頁尺寸@PageIndex int, --頁碼@strWhere varchar(1000), --查詢條件@StrOrder varchar(255), --排序條件@Key varchar(20) --排序欄位asdeclare @strSql varchar(5000) --主語句declare @strSqlCount nvarchar(500)--查詢記錄總數主語句--------------總記錄數---------------if @strWhere !=‘‘beginset @strSqlCount=‘Select COUNT(*) as TotalCout from ‘ + @tbName + ‘ where ‘+ @strWhereendelsebeginset @strSqlCount=‘Select COUNT(*) as TotalCout from ‘ + @tbNameend--------------分頁------------if @PageIndex <= 1beginset@strSql=‘select top ‘+str(@PageSize)+‘ ‘+@tbFields+‘from ‘ + @tbName+ ‘ where ‘ + @strWhere +‘ ‘ + @strOrderendelsebeginset @strSql=‘select top ‘+str(@PageSize)+‘ ‘+@tbFields+‘from ‘ + @tbName + ‘ where ‘ + @strWhere+‘ AND ‘+@Key +‘ > (select MAX(‘+@Key+‘) from ( select top ‘+str((@PageIndex-1)*@PageSize)+‘ ‘+@Key +‘ from ‘ + @tbName + ‘ where ‘ + @strWhere +‘ ‘+@strOrder +‘ )a) ‘+ @strOrderendexec (@strSqlCount)exec(@strSql)GO
【SQL Server】Sql分頁(自用)