sql通用分頁存儲過程
① 一個通用的分頁存儲過程問題
CREATE PROCEDURE X(
@tblName nvarchar(200), ----要顯示的表或多個表的連接
@fldName nvarchar(500) = '*', ----要顯示的欄位列表
@fldSort nvarchar(200) = null, ----排序欄位列表或條件
@ID nvarchar(150), ----主表的主鍵
@pageSize int = 1, ----每頁顯示的記錄個數
@page int = 10, ----要顯示那一頁的記錄
@Sort bit = 0, ----排序方法,0為升序,1為降序(如果是多欄位排列Sort指代最後一個排序欄位的排列順序(最後一個排序欄位不加排序標記)--程序傳參如:' SortA Asc,SortB Desc,SortC ')
@strCondition nvarchar(1000) = null, ----查詢條件,不需where
@Dist bit = 0, ----是否添加查詢欄位的 DISTINCT 默認0不添加/1添加
@pageCount int = 1 output, ----查詢結果分頁後的總頁數
@Counts int = 1 output ----查詢到的記錄數
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar(1000) ----存放動態生成的SQL語句
Declare @strTmp nvarchar(1000) ----存放取得查詢結果總數的查詢語句
Declare @strID nvarchar(1000) ----存放取得查詢開頭或結尾ID的查詢語句
Declare @strSortType nvarchar(10) ----數據排序規則A
Declare @strFSortType nvarchar(10) ----數據排序規則B
Declare @SqlSelect nvarchar(50) ----對含有DISTINCT的查詢進行SQL構造
Declare @SqlCounts nvarchar(50) ----對含有DISTINCT的總數查詢進行SQL構造
if @Dist = 0
begin
set @SqlSelect = 'select '
set @SqlCounts = 'Count(*)'
end
else
begin
set @SqlSelect = 'select distinct '
set @SqlCounts = 'Count(DISTINCT '+@ID+')'
end
if @Sort=0
begin
set @strFSortType=' ASC '
set @strSortType=' DESC '
end
else
begin
set @strFSortType=' DESC '
set @strSortType=' ASC '
end
--------生成查詢語句--------
--此處@strTmp為取得查詢結果數量的語句
if @strCondition is null or @strCondition='' --沒有設置顯示條件
begin
set @sqlTmp = @fldName + ' From ' + @tblName
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
set @strID = ' From ' + @tblName
end
else
begin
set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
end
----取得查詢結果總數量-----
exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
declare @tmpCounts int
if @Counts = 0
set @tmpCounts = 1
else
set @tmpCounts = @Counts
--取得分頁總數
set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
/**//**當前頁大於總頁數 取最後一頁**/
if @page>@pageCount
set @page=@pageCount
--/*-----數據分頁2分處理-------*/
declare @pageIndex int --總數/頁大小
declare @lastcount int --總數%頁大小
set @pageIndex = @tmpCounts/@pageSize
set @lastcount = @tmpCounts%@pageSize
if @lastcount > 0
set @pageIndex = @pageIndex + 1
else
set @lastcount = @pagesize
--//***顯示分頁
if @strCondition is null or @strCondition='' --沒有設置顯示條件
begin
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分數據處理
begin
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
+' order by '+ @fldSort +' '+ @strFSortType+')'
+' order by '+ @fldSort +' '+ @strFSortType
end
else
begin
set @page = @pageIndex-@page+1 --後半部分數據處理
if @page <= 1 --最後一頁數據顯示
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
else
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
+' order by '+ @fldSort +' '+ @strSortType+')'
+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
end
end
else --有查詢條件
begin
if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分數據處理
begin
set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName +' from '+@tblName
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
+' Where (1>0) ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType+')'
+' ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType
end
else
begin
set @page = @pageIndex-@page+1 --後半部分數據處理
if @page <= 1 --最後一頁數據顯示
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
else
set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
+' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
+' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+')'
+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
end
end
------返回查詢結果-----
exec sp_executesql @strTmp
GO
② sql 存儲過程分頁(asp.net)
------------------------------------
--用途:分頁存儲過程
--說明:
------------------------------------
Create
PROCEDURE
[dbo].[UP_GetRecordByPage]
@tblName
varchar(255),
--
表名
@fldName
varchar(255),
--
排序欄位名
@PageSize
int
=
10,
--
頁尺寸
@PageIndex
int
=
1,
--
頁碼
@IsReCount
bit
=
0,
--
返回記錄總數,
非
0
值則返回
@OrderType
bit
=
0,
--
設置排序類型,
非
0
值則降序
@strWhere
varchar(1000)
=
'1=1'
--
查詢條件
(注意:
不要加
where)
AS
declare
@begin
int
,@end
int,@sql
varchar(4000),@order
varchar(5)
set
@begin=@PageSize*
(@PageIndex-1)
+1
set
@end=@begin+@PageSize-1
set
@order=''
if(@OrderType=1)
set
@order='
desc'
set
@sql='
select
*
from
(
select
ROW_NUMBER()
over(order
by
'
+
@fldName
+
@order
+'
)
as
od,
*
from
'+@tblName
+'
where
'
+
@strWhere
+
')
as
tbl
where
od
between
'
+
rtrim(ltrim(str(@begin)))+'
and
'
+
rtrim(ltrim(str(@end)))
exec
(@sql)
if(@IsReCount=1)
exec('select
count(*)
from
'+
@tblName+'
where
'
+@strWhere)
我們老師給我們的.....
③ SQL如何實現數據分頁,要具體語句,謝謝
可以的,用存儲過程
分頁存儲過程如下
CREATE PROCEDURE GetRecordFromPage
@tblName varchar(255), -- 表名
@RetColumns varchar(1000) = '*', -- 需要返回的列,默認為全部
@Orderfld varchar(255), -- 排序欄位名
@PageSize int = 10, -- 頁尺寸
@PageIndex int = 1, -- 頁碼
@IsCount bit = 0, -- 返回記錄總數, 非 0 值則返回
@OrderType varchar(50) = 'asc', -- 設置排序類型, 非 asc 值則降序
@strWhere varchar(1000) = '' -- 查詢條件 (注意: 不要加 where)
AS
declare @strSQL varchar(1000) -- 主語句
declare @strTmp varchar(300) -- 臨時變數
declare @strOrder varchar(400) -- 排序類型
if @IsCount != 0 --執行總數統計
begin
if @strWhere != ''
set @strSQL = "select count(*) as Total from [" + @tblName + "] where " + @strWhere
else
set @strSQL = "select count(*) as Total from [" + @tblName + "]"
end
else --執行查詢操作
begin
if @OrderType != 'asc'
begin
set @strTmp = "<(select min"
set @strOrder = " order by [" + @Orderfld +"] desc"
end
else
begin
set @strTmp = ">(select max"
set @strOrder = " order by [" + @Orderfld +"] asc"
end
set @strSQL = "select top " + str(@PageSize) + " " + @RetColumns + " from ["
+ @tblName + "] where [" + @Orderfld + "]" + @strTmp + "(["
+ @Orderfld + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @Orderfld + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"
+ @strOrder
if @strWhere != ''
set @strSQL = "select top " + str(@PageSize) + " " + @RetColumns + " from ["
+ @tblName + "] where [" + @Orderfld + "]" + @strTmp + "(["
+ @Orderfld + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @Orderfld + "] from [" + @tblName + "] where (" + @strWhere + ") "
+ @strOrder + ") as tblTmp) and (" + @strWhere + ") " + @strOrder
if @PageIndex = 1
begin
set @strTmp = ""
if @strWhere != ''
set @strTmp = " where (" + @strWhere + ")"
set @strSQL = "select top " + str(@PageSize) + " " + @RetColumns + " from ["
+ @tblName + "]" + @strTmp + " " + @strOrder
end
end
exec (@strSQL)
④ 怎樣實現sql資料庫中表的分頁功能,用存儲過程。asp.net 謝謝回答!
create
proc
ProcPageing@pageindex
int,@pagesize
intasbeginselect
top
@pagesize
*
from
TableName
where
ID
not
in(select
(@pageindex-1)*@pagesize
ID
from
TableName
order
by
ID
desc)
oder
by
descendgo
--最簡單的分頁存錯過程
不帶條件、表的
ID號連續(這里可以使用
rows_number()
over(ID
order
by
desc)
as
TID)
這樣
不管資料庫ID是不是連續的ID
這個TID就會是連續的.
⑤ 資料庫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注入