sql T_sql 巧用with做自定义数据表分页方法
2010-07-02 10:50
369 查看
1。依旧是先创建数据库表,并插入相关数据,如果在此之前已经有该表的话,这里就不需要创建了。
CREATE TABLE [dbo].[StudentsScore](
[Student] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Subject] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Score] [int] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
--插入数据记录脚本:
Insert into StudentsScore (Student,Subject,Score) values ( '学生A', '中文', 80 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生A', '数学', 78 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生A', '英语', 92 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生B', '中文', 89 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生B', '数学', 87 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生B', '英语', 75 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生C', '中文', 92 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生C', '数学', 74 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生C', '英语', 65 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生D', '中文', 79 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生D', '数学', 83 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生D', '英语', 81 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生E', '中文', 73 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生E', '数学', 84 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生E', '英语', 93 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生F', '中文', 79 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生F', '数学', 86 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生F', '英语', 84 );
2。先来说说with的用法,请看下面的例子:
![](http://hi.csdn.net/attachment/201007/2/0_1278038916TlWg.gif)
/*with的基本用法*/
with cust as
(
select * from dbo.StudentsScore where score>=80
)
select * from cust
/*运行后返回的是score>=80的数据,如下图:*/
![](http://files.sxtsoft.com/KM/file//c4f4451ffc7049438859f59fdf99d8e2.jpg)
/*
从上面的例子,我们可以发现到,with其实很类似于临时表的作用,这里只是对with的简单应用,
还有相关的功能,请参考sql2005帮助文档的相关介绍。
*/
-----------------------------------------------------------------------
/*
接下来是with加over的用法,over可以用来做自增序列号(虚拟的),行号的唯一标识。
*/
/*with加over用法*/
with cust as
(
select row_number() over (order by student) as 序号,* from dbo.StudentsScore
)
select * from cust
/*运行后效果如下图,我们发现,它自动产生了一列像自增行号的列,这也是我们做分页的依据*/
3。接下来是分页的存储过程实现:
/*我自己做的分页新例子*/
alter proc proc_SplitPages
(
@tb varchar(1000),--表名
@tbSoftField varchar(100),--排序的字段
@pagesize int,--页面条数
@pagecount int--第几页
)
as
begin
declare @start int
declare @end int
set @start = (@pagecount-1)*@pagesize+1;
set @end = @pagecount*@pagesize;
declare @sql varchar(max)
set @sql=''
set @sql='
with cust as
(
select row_number() over (order by '+@tbSoftField+') as rownumber, * from '+@tb+'
)
select * from cust where rownumber between '''+cast(@start as varchar(100))+''' and '''+cast(@end as varchar(100))+'''
'
exec (@sql)
end
/*
下面是每页10条数据的第1页
exec proc_SplitPages 'StudentsScore','student',10,1
效果如图:
![](http://hi.csdn.net/attachment/201007/2/0_1278038954Ojz7.gif)
![](http://hi.csdn.net/attachment/201007/2/0_1278038996w832.gif)
*/
备注:这里的分页是动态的,可以接收任意数据表,对其进行分页,也就是说,我们无需再对
别的数据表做专门的分页功能,直接可以运用此存储过程,即可以达到分页的效果。
下面是每页10条数据的第2页
exec proc_SplitPages 'StudentsScore','student',10,2
效果如图:
CREATE TABLE [dbo].[StudentsScore](
[Student] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Subject] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Score] [int] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
--插入数据记录脚本:
Insert into StudentsScore (Student,Subject,Score) values ( '学生A', '中文', 80 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生A', '数学', 78 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生A', '英语', 92 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生B', '中文', 89 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生B', '数学', 87 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生B', '英语', 75 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生C', '中文', 92 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生C', '数学', 74 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生C', '英语', 65 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生D', '中文', 79 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生D', '数学', 83 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生D', '英语', 81 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生E', '中文', 73 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生E', '数学', 84 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生E', '英语', 93 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生F', '中文', 79 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生F', '数学', 86 );
Insert into StudentsScore (Student,Subject,Score) values ( '学生F', '英语', 84 );
2。先来说说with的用法,请看下面的例子:
![](http://hi.csdn.net/attachment/201007/2/0_1278038916TlWg.gif)
/*with的基本用法*/
with cust as
(
select * from dbo.StudentsScore where score>=80
)
select * from cust
/*运行后返回的是score>=80的数据,如下图:*/
![](http://files.sxtsoft.com/KM/file//c4f4451ffc7049438859f59fdf99d8e2.jpg)
/*
从上面的例子,我们可以发现到,with其实很类似于临时表的作用,这里只是对with的简单应用,
还有相关的功能,请参考sql2005帮助文档的相关介绍。
*/
-----------------------------------------------------------------------
/*
接下来是with加over的用法,over可以用来做自增序列号(虚拟的),行号的唯一标识。
*/
/*with加over用法*/
with cust as
(
select row_number() over (order by student) as 序号,* from dbo.StudentsScore
)
select * from cust
/*运行后效果如下图,我们发现,它自动产生了一列像自增行号的列,这也是我们做分页的依据*/
3。接下来是分页的存储过程实现:
/*我自己做的分页新例子*/
alter proc proc_SplitPages
(
@tb varchar(1000),--表名
@tbSoftField varchar(100),--排序的字段
@pagesize int,--页面条数
@pagecount int--第几页
)
as
begin
declare @start int
declare @end int
set @start = (@pagecount-1)*@pagesize+1;
set @end = @pagecount*@pagesize;
declare @sql varchar(max)
set @sql=''
set @sql='
with cust as
(
select row_number() over (order by '+@tbSoftField+') as rownumber, * from '+@tb+'
)
select * from cust where rownumber between '''+cast(@start as varchar(100))+''' and '''+cast(@end as varchar(100))+'''
'
exec (@sql)
end
/*
下面是每页10条数据的第1页
exec proc_SplitPages 'StudentsScore','student',10,1
效果如图:
![](http://hi.csdn.net/attachment/201007/2/0_1278038954Ojz7.gif)
![](http://hi.csdn.net/attachment/201007/2/0_1278038996w832.gif)
*/
备注:这里的分页是动态的,可以接收任意数据表,对其进行分页,也就是说,我们无需再对
别的数据表做专门的分页功能,直接可以运用此存储过程,即可以达到分页的效果。
下面是每页10条数据的第2页
exec proc_SplitPages 'StudentsScore','student',10,2
效果如图:
相关文章推荐
- php自定义方法:自动生成数据表格(支持分页)
- SQL数据分页查询的方法
- 分页解决方案 之 QuickPager的使用方法(PostBack分页、自定义获取数据)
- ASP.NET 数据列表控件的分页总结(一)-----自定义方法分页和PageDataSource类分页
- SQL 语句 数据分页方法
- ASP.NET 数据列表控件的分页总结(一)-----自定义方法分页和PageDataSource类分页(转)
- 分页解决方案 之 QuickPager的使用方法(PostBack分页、自定义获取数据)
- SQL数据分页查询的方法
- SQL Server游标 C# DataTable.Select() 筛选数据 什么是SQL游标? SQL Server数据类型转换方法 LinQ是什么? SQL Server 分页方法汇总
- 用@Query标注自定义查询语句用原生sql查询符合时间要求的数据与分页等功能
- SQL Server中删除重复数据的几个方法
- JDBC操作 SQL SERVER之数据的分页显示
- android 分页数据 LinkedHashSet及ArrayList两种去重方法
- 自己实现的数据表格控件(dataTable),支持自定义样式和标题数据、ajax等各种自定义设置以及分页自定义
- 巧用SQL的with语法生成临时结果集,进行查询与更新同步操作,解决高并发下对数据的重复访问
- SQL跨数据库读取数据的方法
- 三种高效率SQL语句分页方法
- 用归档保存的对象,其子对象必须也支持归档包括自定义的对象。及实现initWithCoder和encodeWithCoder方法
- 解决错误:sql_variantisincompatiblewithxml(ASP.NET2.0/XML数据类型)
- 给栅格数据添加RasterFunction--自定义渲染方法