㈠ sqlserver top 分頁是怎麼實現的
分頁通過當前頁碼,每頁記錄數算出當前 頁的記錄的最大值最小值
然後
selecttop(最大值)*from表where標識>最小值
㈡ sqlserver 2005資料庫 怎麼進行分頁
分兩步實現
一、分頁的存儲過程如下
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE [dbo].[Pagination]
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) , -- 需要返回的列
@fldName varchar(255), -- 排序的欄位名
@PageSize int, -- 頁尺寸
@PageIndex int , -- 頁碼
@doCount bit=0, -- 返回記錄總數, 非 0 值則返回
@OrderType bit, -- 設置排序類型, 非 0 值則降序
@strWhere varchar(1500) -- 查詢條件 (注意: 不要加 where)
AS
declare @strSQL varchar(5000) -- 主語句
declare @strTmp varchar(110) -- 臨時變數
declare @strOrder varchar(400) -- 排序類型
if @doCount != 0
begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where ' + @strWhere
else
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'
end
--以上代碼的意思是如果@doCount傳遞過來的不是0,就執行總數統計。以下的所有代碼都是@doCount為0的情況
else
begin
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName + '] desc'
end
--如果@OrderType不是0,就執行降序,這句很重要!
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName + '] asc'
end
if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @strGetFields + ' from [' + @tblName + '] where '
+ @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @strGetFields
+ ' from [' + @tblName + '] ' + @strOrder
end
--如果是第一頁就執行以上代碼,這樣會加快執行速度
else
begin
--以下代碼賦予了@strSQL以真正執行的SQL代碼
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @strGetFields + ' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top '
+ str( ( @PageIndex - 1 ) * @PageSize ) + ' ['+ @fldName + '] from [' + @tblName + ']'
+ @strOrder + ') as tblTmp)' + @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @strGetFields + ' from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str( ( @PageIndex - 1 ) * @PageSize ) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
end
exec(@strSQL)
二、頁面調用部分代碼
Function navindex(ByVal PageIndextemp As Integer, ByVal PageSizetemp As Integer, ByVal countint As Integer, ByVal pagename As String) As String
Dim i As Integer
If countint Mod PageSizetemp = 0 Then
i = countint \ PageSizetemp
Else
i = countint \ PageSizetemp + 1
End If
Dim maxi, mini As Integer
Dim navleft, navright, navstrtemp As String
If i < 10 Then
maxi = i
mini = 1
Else
maxi = pageindex + 3
mini = pageindex - 3
If mini > 1 Then
navleft = "<a href=""" & pagename & "?page=" & (mini - 1) & """ class=""link_nav_btn""><</a> "
Else
mini = 1
maxi = 10
End If
If maxi < i Then
navright = " <a href=""" & pagename & "?page=" & (maxi + 1) & """ class=""link_nav_btn"">></a>"
Else
If i - 10 > 0 Then
mini = i - 10
Else
mini = 1
End If
maxi = i
End If
End If
For n As Integer = mini To maxi
If n = pageindex Then
navstrtemp = navstrtemp & " <a href=""" & pagename & "?page=" & n & """ class=""link_nav_btn_select""><b>" & n & "</b></a>"
Else
navstrtemp = navstrtemp & " <a href=""" & pagename & "?page=" & n & """ class=""link_nav_btn"">" & n & "</a>"
End If
Next
navstrtemp = navleft & navstrtemp & navright
Return navstrtemp
End Function
Sub databinds(ByVal tblnametemp As String, ByVal strGetFieldstemp As String, ByVal fldNametemp As String, ByVal PageSizetemp As Integer, ByVal PageIndextemp As Integer, ByVal OrderTypetemp As Short, ByVal strWheretemp As String)
'tblnametemp表名,strGetFieldstemp需要返回的列,fldNametemp排序的欄位名,PageSizetemp頁尺寸,PageIndextemp頁碼,OrderTypetemp設置排序類型,strWheretemp查詢條件
'總數
cmdTM = New SqlCommand("select count(*) from " & tblnametemp & " where " & strWheretemp, conPubs)
conPubs.Open()
countint = CInt(cmdTM.ExecuteScalar())
conPubs.Close()
'導航
navstr = navindex(PageIndextemp, PageSizetemp, countint, "newshyxh.aspx")
'分頁
cmdTM = New SqlCommand("Pagination", conPubs)
cmdTM.CommandType = CommandType.StoredProcere
'add input
cmdTM.Parameters.Add("@tblName", SqlDbType.VarChar, 255).Value = tblnametemp
cmdTM.Parameters.Add("@strGetFields", SqlDbType.VarChar, 1000).Value = strGetFieldstemp
cmdTM.Parameters.Add("@fldName", SqlDbType.VarChar, 255).Value = fldNametemp
cmdTM.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndextemp
cmdTM.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSizetemp
cmdTM.Parameters.Add("@OrderType", SqlDbType.Bit).Value = OrderTypetemp
cmdTM.Parameters.Add("@strWhere", SqlDbType.VarChar, 1500).Value = strWheretemp
conPubs.Open()
newsright.DataSource = cmdTM.ExecuteReader()
newsright.DataBind()
conPubs.Close()
End Sub
㈢ SQLSERVER如何實現分頁查詢
寫存儲過程 ..
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER PROCEDURE usp_Province_pagination
@PageSize INT, --每頁的顯示的行數
@AbsolutePage INT, -- 當前頁的頁數
@PageCount INT OUTPUT --總頁數
AS
DECLARE @BeginRecord INT --記錄每此從哪一行開始讀取
DECLARE @RecordCount INT --表中數據的總條數
DECLARE @sql NVARCHAR(1000)
SET @RecordCount = (SELECT count(*) FROM Province)
--表中沒有數據的情況
IF @RecordCount = 0
BEGIN
SET @PageCount = 0
RETURN(0)
END
-- 表中的總條數大於定義的每頁的行數的情況
IF @RecordCount > @PageSize
BEGIN
-- 計算總能分多少頁
SET @PageCount = (@RecordCount + @PageSize - 1)/@PageSize
--當前應該從哪一行開始讀取
SET @BeginRecord = ((@AbsolutePage-1) * @PageSize)
SET @sql = N'SELECT TOP ' + cast(@PageSize AS NVARCHAR(100)) +' ProvinceID, provinceCode, ProvinceName
FROM Province
WHERE ProvinceID NOT IN
(SELECT TOP '+ CAST(@BeginRecord AS NVARCHAR(100)) + ' ProvinceID
FROM Province)'
EXECUTE sp_executesql @sql
END
ELSE -- -- 表中的總條數大於定義的每頁的行數情況
BEGIN
SET @PageCount = 1
SET @SQL = 'SELECT ProvinceID, provinceCode, ProvinceName FROM Province '
EXECUTE sp_executesql @sql
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
㈣ SQL Server 分頁 查詢語句
四種方式實現SQLServer 分頁查詢
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
(
--40是這么計算出來的:10*(5-1)
select top 40 id from test order by id
)
order by id
原理:需要拿出資料庫的第5頁,就是40-50條記錄。首先拿出資料庫中的前40條記錄的id值,然後再拿出剩餘部分的前10條元素
第二種方法:
還是以上面的結果為例,採用另外的一種方法
--數據的意思和上面提及的一樣
select top 10 *
from test
where id >
(
select isnull(max(id),0)
from
(
select top 40 id from test order by id
) A
)
order by id
原理:先查詢前40條記錄,然後獲得其最id值,如果id值為null的,那麼就返回0
然後查詢id值大於前40條記錄的最大id值的記錄。
這個查詢有一個條件,就是id必須是int類型的。
第三種方法:
select top 10 *
from
(
select row_number() over(order by id) as rownumber,* from test
) A
where rownumber > 40
原理:先把表中的所有數據都按照一個rowNumber進行排序,然後查詢rownuber大於40的前十條記錄
這種方法和oracle中的一種分頁方式類似,不過只支持2005版本以上的
第四種:
存儲過程查詢
創建存儲過程
alter procere pageDemo
@pageSize int,
@page int
AS
declare @temp int
set @temp=@pageSize*(@page - 1)
begin
select top (select @pageSize) * from test where id not in (select top (select @temp) id from test) order by id
end
執行存儲過程
exec 10,5
㈤ sqlserver 聚合 如何分頁
我看沒人回答我再上。
-----------------------------
這個用子查詢就可以了!如果嵌套很多,證明你的資料庫設計很蹩腳。
給你個例子:
----------
SELECT TOP 10
convert(varchar(10),K.ID) as ID,
convert(varchar(20),convert(decimal(18,2),K.SumRealSaleCost)) as SumRealSaleCost,
K.CountRes
from
(
select
ROW_NUMBER() OVER (ORDER BY GetDate()) AS ID,
(sum(M.Sale) over()-sum(M.ReturnSaleCost) over()) as SumRealSaleCost,
count(*) over() as CountRes
from
(
---這里你愛用什麼聚合函數就用什麼聚合函數,我只用了一個簡單的子查詢而已!
select
A.Sale,
A.ReturnSaleCost
from ProctStore A
) M
) K
where K.ID >?
--這個排序最好和分頁函數一致。
ORDER BY K.Sale DESC
--------------------
最外層為類型轉化層
中間是數據邏輯層
最內層就是集合構建層
我這種寫法很多人不理解,本人原創。你網路不到!
---------------------
如果LZ資料庫有功底的話,加入我的團隊吧!資料庫聚賢庄
㈥ 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
最後說一句:我沒有測試環境,如果有編寫錯誤,請及時提醒我,謝謝。
㈦ 資料庫sqlserver如何用存儲過程做分頁
存儲過程:create Procere pname
( @pageIndex int,@pageSize)
as
select * from tableName order by id
offset @pageIndex * pageSize fetch next pageSize rows only
分頁:
sqlserver 在2008之前 使用 top 和 not int top 的方式來做分頁
2008以後使用 row_number() 函數作為分頁關鍵函數
2012使用 offset 1 fetch next 10 rows only
你問了2個問題,你可以優先把視圖,存儲過程,觸發器等弄明白,分頁是查詢,在存儲過程里可以寫復雜的sql文,只是在運行時是預編譯和參數化查詢防止sql注入
㈧ SqlServer分頁
分頁的原理無非是根據傳遞的頁數和每頁行數算出從第幾行開始取多少行數據。所以根據你的參數你完全可以任意取數。
㈨ 如何使用sql語句進行分頁操作
利用SQL語句分頁要看你用的什麼資料庫。
Oracle資料庫可以使用ROWNUM或row_number(),例如:Select
*
from
(select
ROWNUM
rn,
t.*
from
table
t)
where
rn
between
11
and
20;
Select
*
from
(select
row_number()
over
(ORDER
BY
col1)
rn,
t.*
from
table
t)
where
rn
between
11
and
20;
SQLServer資料庫可以用Top或者row_number()函數,道理同上。
利用SQL分頁有局限性,就是針對不同的資料庫有不同的寫法,所以通常會在應用程序裡面做分頁通用性比較強。但是對於數據量非常龐大的應用來說,還是用SQL分頁比較適合。