if exists (select * from sys.databases where name='test')
drop database test
create database test
go
use test
go
/----创建 员工信息表(employeeInfo)
if exists(select * from sys.tables where name='employeeInfo' )
drop table employeeInfo
create table employeeInfo(
empId int identity(1,1) primary key,
empname varchar(50),
sex nvarchar(2),
age int
)
go
select * from employeeInfo
go
/---创建十万条测试数据 插入数据存储过程
if exists(select * from sys.procedures where name='proDataInsertIntoEmployeeInfo')
drop procedure proDataInsertIntoEmployeeInfo
go
create procedure proDataInsertIntoEmployeeInfo
as
declare @dataCount int ,@i int
select @dataCount=100000,@i=1
while(@i<=@dataCount)
begin
insert into employeeInfo values((char (rand()*100))+(char (rand()*100))+(char (rand()*100)),'男',rand()*50)
set @i=@i+1
end
go
execute proDataInsertIntoEmployeeInfo
/--产生随字符
select (char (rand()*100))+(char (rand()*100))+(char (rand()*100))
/--产生随即1-50的数字
select rand()*50
select * from employeeInfo
go
/--创建得到 员工信息表总记录数存储过程selectEmployeeInfoCount
if exists(select * from sys.procedures where name='selectEmployeeInfoCount')
drop procedure selectEmployeeInfoCount
go
create procedure selectEmployeeInfoCount
as
declare @EmployeeCount int
select @EmployeeCount=count(*) from employeeInfo
return @EmployeeCount
go
execute selectEmployeeInfoCount
/---创建员工信息分页 存储过程 (not in ) 存储过程
if exists(select * from sys.procedures where name='EmployeeInfoPage')
drop procedure EmployeeInfoPage
go
create procedure EmployeeInfoPage
@startRowIndex int=1 ,
@pageSize int=10
as
select top (@pageSize) * from employeeInfo where empId not in
(select top (@pageSize*(@startRowIndex-1)) empId from employeeInfo order by empId )
order by empId
go
execute EmployeeInfoPage 10000,10
go
/--创建 排序储存过程
if exists(select * from sys.procedures where name='EmployeeInfoPageByDesc')
drop procedure EmployeeInfoPageByDesc
/--创建存储过程(EmployeeInfoPageByDesc)
create procedure EmployeeInfoPageByDesc
@startRowIndex int=1 ,
@pageSize int=10
as
declare @recordCount int
execute @recordCount=selectEmployeeInfoCount
select top(@pageSize) * from
(select top (@recordCount-(@startRowIndex-1)*@pageSize) * from employeeInfo order by empId desc)
emp
order by empId asc
go
分享到:
相关推荐
总结来说,SQL分页有多种策略,包括`ROW_NUMBER()`方法、`NOT IN`与`SELECT TOP`组合、以及游标存储过程等。选择哪种方法取决于数据量、查询复杂度和性能需求。在大部分情况下,`ROW_NUMBER()`方法是最优选择,因为...
总结来说,SQL Server存储过程实现单条件分页的方法主要包括以下几个步骤:构建SQL语句,计算总记录数,确定总页数,设置排序方式,以及执行分页查询。这样的存储过程不仅可以提高查询效率,还能降低网络传输的数据...
##### 方案三:利用SQL的游标存储过程分页 该方案使用存储过程和游标来实现分页查询。虽然通用性较强,但性能通常不如前两种方案。 ```sql CREATE PROCEDURE XiaoZhengGe @sqlstr nvarchar(4000), -- 查询字符串 @...
然后,我们插入2万条数据以进行测试。在实际应用中,数据量可能会更大,这将更明显地体现出不同分页方法的性能差异。 **分页方案一:Not In + SELECT TOP** 此方法基于`NOT IN`子句和`SELECT TOP`。基本思路是首先...
13、说明:一条sql 语句搞定数据库分页 select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段 具体实现: ...
编写SQL语句进行数据插入、更新和查询,实现数据的持久化存储。 7. 分页爬取:51job网站的职位信息可能分布在多个页面,需要编写逻辑处理分页,递归或循环地爬取每个页面,直至获取所有目标数据。 8. 异常处理:在...
在SQL Server中,多表查询优化对于提升数据库性能至关重要,特别是在大数据量的环境中。本文将深入探讨几个关键的优化策略,以帮助改善SQL Server多表查询的效率。 1. **执行计划优化**:执行计划是SQL Server解析...
13、说明:一条sql 语句搞定数据库分页 select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段 具体实现: ...
首先,为了模拟真实的数据环境,讲师通过一系列步骤创建了两个表——`class`(班级表)和`student`(学生表),并填充了50万条学生数据和1万条班级数据。接下来,我们深入讲解这两个核心主题。 1. **索引优化**: ...
329 实例238 列出系统中的打印机 330 7.12 其他 332 实例239 两种信息发送方式 332 实例240 功能快捷键 336 第8章 注册表 339 8.1 操作注册表 340 实例241 怎样存取注册表信息 ...
对于1万条数据,这种方式耗时约6分钟7秒,如果插入1000万条,可能需要几天。 2. **带有事务的插入**:尝试在每1000条数据后提交一次事务,期望能提升性能。例如: ```sql DELIMITER // CREATE PROCEDURE u_head_...
实例012 带进度条的状态栏 10 实例013 状态栏中加入图标 11 1.4 导航菜单界面 11 实例014 OutLook界面 11 实例015 带导航菜单的主界面 12 实例016 图形化的导航界面 14 1.5 特色程序界面 15 实例017 隐藏式...