SQLSERVER 分页存储过程(2 在SQL2005下使用)
之前有一个SQLServer的分页存储过程 但是性能不是十分理想
又找了一个
--SQL2005分页存储过程
/**
if exists(select * from sysobjects where name='fenye')
drop proc fenye
**/
CREATE procedure fenye
@tableName nvarchar(200) ,
@pageSize int,
@curPage int ,
@orderBy nvarchar(200) ,
@field nvarchar(200) = '*' ,
@condition nvarchar(200)
AS
SET NOCOUNT ON
DECLARE
@STMT nvarchar(max), -- SQL to execute
@recct int -- total # of records (for GridView paging interface)
IF LTRIM(RTRIM(@condition)) = '' SET @condition = '1 = 1'
IF @pageSize IS NULL BEGIN
SET @STMT = 'SELECT ' + @field + 'from ' + @tableName +'WHERE ' + @condition + 'ORDER BY ' + @orderBy
EXEC (@STMT) -- return requested records
END ELSE BEGIN
SET @STMT = 'SELECT @recct = COUNT(*) from ' + @tableName + ' WHERE ' + @condition
-- EXEC sp_executeSQL @STMT, @params = N'@recct INT OUTPUT', @recct = @recct OUTPUT
--SELECT @recct AS recct -- return the total # of records
DECLARE
@lbound int,
@ubound int
SET @curPage = ABS(@curPage)
SET @pageSize = ABS(@pageSize)
IF @curPage < 1 SET @curPage = 1
IF @pageSize < 1 SET @pageSize = 1
SET @lbound = ((@curPage - 1) * @pageSize)
SE
相关文档:
如果你经常遇到下面的问题,你就要考虑使用SQL Server的模板来写规范的SQL语句了:
SQL初学者。
经常忘记常用的DML或是DDL SQL 语句。
在多人开发维护的SQL中,每个人都有自己的SQL习惯,没有一套统一的规范。
在SQL Server Management Studio中,已经给大家提供了很多常用的现成SQL规范模板。
SQL Server Management ......
--TOP n 实现的通用分页存储过程(转自邹建)
CREATE PROC sp_PageView
@tbname sysname, --要分页显示的表名
@FieldKey nvarchar(1000), --用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段
@PageCurrent int=1, --要显示的页码
@PageSize int=10, - ......
=================分页==========================
/*分页查找数据*/
CREATE PROCEDURE [dbo].[GetRecordSet]
@strSql varchar(8000),--查询sql,如select * from [user]
@PageIndex int,--查询当页号
@PageSize int--每页显示记录
AS
set nocount on
declare @p1 int
declare @current ......
SQLServer2005通过intersect,union,except和三个关键字对应交、并、差三种集合运算。
他们的对应关系可以参考下面图示
相关测试实例如下:
use tempdb
go
if (object_id ('t1' ) is not null ) drop table t1
if (object_id ('t2' ) is not null ) drop table t2
go
cre ......