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
相关文档:
由于工作需求,要对负责的产 品做点性能优化,在网上找到了相关的东西,拿 出来与大家分享:
看到很多朋友对数据库的理解、认识还是没有突破一个瓶颈,而这个瓶颈往往只是一层窗纸,越过了你将看到一个新世界。
04、05年做项目的时候,用SQL Server 2000,核心表(大部分使用频繁的关键功能每次都要用到)达到了800万数据 ......
如果你经常遇到下面的问题,你就要考虑使用SQL Server的模板来写规范的SQL语句了:
SQL初学者。
经常忘记常用的DML或是DDL SQL 语句。
在多人开发维护的SQL中,每个人都有自己的SQL习惯,没有一套统一的规范。
在SQL Server Management Studio中,已经给大家提供了很多常用的现成SQL规范模板。
SQL Server Management ......
我在把oracle数据导入sqlserver中时,发现在oracle中字段定义为唯一索引时,不同记录的此字段如果为空不被认为是重复的,但在sqlserver中如果此字段为唯一索引字段,不允许有2个以上的空值。郁闷。所以只好将sqlserver中的唯一索引字段手工修改为几个非空的值,但这样程序肯定要进行修改了。需要在程序中为此字段设置不重复 ......
--TOP n 实现的通用分页存储过程(转自邹建)
CREATE PROC sp_PageView
@tbname sysname, --要分页显示的表名
@FieldKey nvarchar(1000), --用于定位记录的主键(惟一键)字段,可以是逗号分隔的多个字段
@PageCurrent int=1, --要显示的页码
@PageSize int=10, - ......
sqlserver字符串拆分(split)方法汇总
--方法0:动态SQL法
declare @s varchar(100),@sql varchar(1000)
set @s='1,2,3,4,5,6,7,8,9,10'
set @sql='select col='''+ replace(@s,',',''' union all select ''')+''''
PRINT @sql
exec (@sql)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ ......