當前位置:首頁 » 編程語言 » sqlserver2014分頁
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sqlserver2014分頁

發布時間: 2022-10-05 02:13:58

sqlserver中怎麼分頁查詢

假設現在有這樣的一張表:
CREATE TABLE test
(
id int primary key not null identity,
names varchar(20)
)
然後向裡面插入大約1000條數據,進行分頁測試
假設頁數是10,現在要拿出第5頁的內容,查詢語句如下:
--10代表分頁的大小
select top 10 *
from test
where id not in

Ⅱ sql server 分頁

with a as(
select *,row_number over(order by id) rn from 表 where workernumber ='012'
)
select * from a where rn>1 and rn<100

Ⅲ sql server 怎麼寫分頁的sql語句

ID:唯一標示列
number:一次讀取的行數
page:第幾頁

select top(number)ID,欄位 from 表 where 條件x And ID Not In (select top(number*page)ID,欄位 from 表 where 條件X)

Ⅳ SQL Server數據分頁的問題

//得到頁號
int pageId=Integer.parseInt(request.getParameter("pageId"));

int begin=(pageId-1)*pageSize;
String select =request.getParameter("select");

String sqlLimit="";//分頁sql語句
String sqlCount="";//總行數sql語句
ShowTravleInfoManager sm = new ShowTravleInfoManager();

//無條件分頁查詢所有景點信息
if(select.equals("all"))
{
sqlCount = "select count(*) from travleInfo";
sqlLimit="select top "+pageSize+" * from travleInfo t,areaInfo a where t.travleId not in " +
"(select top "+begin+" travleId from travleInfo) and t.areaId=a.areaId order by t.travleId";
}

一:分頁的分類:
分頁有真分頁和假分頁兩種,

假分頁即把所有數據一次性提取到內存中.再分頁.
缺點:數據量太大.訪問慢.

真分頁則用SQL語句在資料庫中提取本頁數據到內存中. 缺點:程序不好移植.

二: 真分頁作法:
一般是通過sql語句查詢出指定頁號的數據 .請注意資料庫支持的關鍵字

1. sql server 用 top :前幾行
2. oracle 用 rownum :行號之差
3. Mysql 用 limit ; 行號之差

三:測試:
表:
create table stuInfo-- 學生信息表
(
stuId varchar(20) primary key,
stuName varchar(20),
stuSex varchar(8),
stuAddress varchar(40)
);
insert into stuInfo values('A001','xie','男','湖南長沙');
insert into stuInfo values('A002','張飛','男','湖南長沙');
insert into stuInfo values('A003','王熙鳳','男','湖南長沙');
insert into stuInfo values('A004','李逵','男','湖南長沙');
insert into stuInfo values('A005','張力','男','湖南長沙');
insert into stuInfo values('A006','張飛','男','湖南長沙');
insert into stuInfo values('A007','王鳳','男','湖南長沙');
insert into stuInfo values('A008','李達','男','湖南長沙');
insert into stuInfo values('A009','xie','男','湖南長沙');
insert into stuInfo values('A010','張飛','男','湖南長沙');
insert into stuInfo values('A011','王熙鳳','男','湖南長沙');
insert into stuInfo values('A012','李逵','男','湖南長沙');
insert into stuInfo values('A013','張力','男','湖南長沙');
insert into stuInfo values('A014','張飛','男','湖南長沙');
insert into stuInfo values('A015','王鳳','男','湖南長沙');
insert into stuInfo values('A016','李達','男','湖南長沙');
select * from stuInfo;

在:MSSQL2005中的寫法:
select top 5 * from stuInfo
where stuId not in
(select top 5 stuId from stuInfo)
order by stuId
--5:代表第2頁

在:oracle中的寫法:
--查詢第1頁數據. (當前頁-1)*每頁幾行
select * from stuInfo where rownum<=1*5
minus
select * from stuInfo where rownum<=0*5;

在mysql中的寫法:
select * from stuInfo limit 0,5;
select * from stuInfo limit 5,10;

具體用法:
一:在javaBean中,返回一個bean對象集合
public List getProctById(String id,int pageId,int pageNum)
{
Connection conn=null;
try
{ ArrayList li=new ArrayList();
int begin=(pageId-1)*pageNum;
String sql="select * from item where proctid='"+id
+"' limit "+begin+","+pageNum+"";
conn= ShopDB.getConnection();
Statement st= conn.createStatement();
java.sql.ResultSet rs= st.executeQuery(sql) ;
while(rs.next())
{
Item myitem=new Item();
myitem.setItemid(rs.getString("itemid"));
myitem.setListprice(rs.getFloat("listprice"));
myitem.setUnitcost(rs.getFloat("unitcost"));
myitem.setName(rs.getString("name"));
myitem.setDescrition(rs.getString("descrition"));
myitem.setImagepath(rs.getString("imagepath"));
li.add(myitem);
}
return li;
}
catch (Exception e) {
e.printStackTrace();
return null;
}finally{
ShopDB.colseConn(conn) ;

}
二:hibernate中:返回一個二維數組集合.
//真分頁方法
public List findUserPageData(int pageId,int pageNum) {
try {
int comp=(pageId-1)*pageNum;
String sql = "select * from Userinfo limit "+comp+","+pageNum;
System.out.println(sql);
SQLQuery queryObject = getSession().createSQLQuery(sql);
return queryObject.list();
} catch (RuntimeException re) {
throw re;
}
}
public static void main(String[] args) {
try {
UserinfoDAO = new UserinfoDAO();
List list=.findUserPageData(1, 2);
System.out.println("大小:"+list.size());
for (int i = 0; i < list.size(); i++) {
Object kk[] =(Object[]) list.get(i);
System.out.println("姓名" + kk[1]);
}

Ⅳ SQLServer分頁

create procere proc_dividePage
@pageIndex int, --用戶點頁數
@pageSize int, --每頁顯示多少行數據
@pageCount int output --總共顯示多少頁
as
declare @firstIndex int
declare @lastIndex int
declare @total int
set @firstIndex=(@pageIndex-1)*@pageSize+1
set @lastIndex=@pageSize*@pageIndex
select @total=count(*) from 表名
if(@total%@pageSize<>0)
begin
set @pageCount=@total/@pageSize+1
end
else
begin
set @pageCount=@total/@pageSize
end
select * from
(select row_number() over(order by 主鍵) as newColumn,* from 表名) as newTable
where newColumn between @firstIndex and @lastIndex

最後說一句:我沒有測試環境,如果有編寫錯誤,請及時提醒我,謝謝。