sql 2005 分页,排名函数
WITH V AS(SELECT ROW_NUMBER() OVER(order by fcustid desc) AS RN,* from ts_dict AS SourceView)
SELECT * from V WHERE RN BETWEEN 1 AND 10
转载地址:http://www.cnblogs.com/nokiaguy/archive/2009/02/05/1384860.html
排名函数是SQL Server2005新加的功能。在SQL Server2005中有如下四个排名函数:
1. row_number
2. rank
3. dense_rank
4. ntile
下面分别介绍一下这四个排名函数的功能及用法。在介绍之前假设有一个t_table表,表结构与表中的数据如图1所示:
图1
其中field1字段的类型是int,field2字段的类型是varchar
一、row_number
row_number函数的用途是非常广泛,这个函数的功能是为查询出来的每一行记录生成一个序号。row_number函数的用法如下面的SQL语句所示:
select row_number() over(order by field1) as row_number,* from t_table
上面的SQL语句的查询结果如图2所示。
图2
其中row_number列是由row_number函数生成的序号列。在使用row_number函数是要使用over子句选择对某一列进行排序,然后才能生成序号。
实际上,row_number函数生成序号的基本原理是先使用over子句中的排序语句对记录进行排序,然后按着这个顺序生成序号。over子句中的order by子句与SQL语句中的order by子句没有任何关系,这两处的order by 可以完全不同,如下面的SQL语句所示:
select row_number() over(order by field2 desc) as row_number,* from t_table order by field1 desc
上面的SQL语句的查询结果如图3所示。
图3
我们可以使用row_number函数来实现查询表中指定范围的记录,一般将其应用到Web应用程序的分页功能上。下面的SQL语句可以查询t_table表中第2条和第3条记录:
with t_rowtable
as
(
select row_number() over(order by field1) as row_number,* from t_table
)
select * from t_rowtable where row_number>1 and row_number < 4 order by f
相关文档:
将Excel文件数据库导入SQL Server的三种方案
//方案一: 通过OleDB方式获取Excel文件的数据,然后通过DataSet中转到SQL Server
openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel files(*.xls)|*.xls";
if(openFileDialog.ShowDialog()==DialogResult.OK)
{
FileInfo ......
一直都想找个时间研究一下,今天花了一天时间去实践了一下,非常有用。可以说是数据库里面的精华吧。还好我今天去弄了几下,虽说都是些简单的语句,但是最起码知道以后在项目该怎么去做了。 闲话少说。进入正题。
1.创建SQL存储过程:
CREATE PROCEDURE stu_proc1//指定过程名
@Sno varc ......
Introducing Oracle Database 11g
List the features of Oracle Database 11g
Discuss the basic design, theoretical and physical aspects of a relational database
Categorize the different types of SQL statements
Describe the data set used by the course
Log onto the database using the SQL Develope ......
shopxp网上购物系统 v7.4存在Sql注入漏洞。
问题文件:xpCatalog_xpDesc.asp,xpCatalog_xpsmall_Desc.asp
问题代码:
<%
dim shopxpbe_id, anclassname, shopxpse_id, nclassname
dim totalPut
dim CurrentPage, TotalPages
if request("shopxpbe_id")<>"" then
shopxpbe_id=request("shopxpbe_id")
e ......
一、加快sql的执行速度
1.select 语句中使用sort,或join
如果你有排序和连接操作,你可以先select数据到一个临时表中,然后再对临时表进行处理。因为临时表是建立在内存中,所以比建立在磁盘上表操作要快的多。
如:
SELECT time_records.*, case_name
from time_records, OUTER cases
WHERE time_re ......