set ansi_nulls on
set quoted_identifier on
go/*
支援多表查詢分頁儲存過程(事理改進)2012.3
--多表聯查1
declare @count int
exec [proc_datapagination] 'sl_article a,sl_user u','u.realname,a.*','a.userid=u.userid','',1,20,0,@count output
select @count
--多表聯查2
declare @count int
exec proc_datapagination 'sl_lanandwanpermissionlog l left join sl_plate p on l.plateid=p.plateid left join sl_admin a on l.adduserid=a.userid','l.*,p.platename,a.realname as adduserrealname','','id',1,20,0,@count output
select @count
*//*注意:多表聯查,如果兩個表有相同的列名,必須指定要查詢的列名,不然會報錯*/
alter procedure [dbo].[proc_datapagination]
(@table nvarchar(1000),--表名,支援多表聯查
@fields nvarchar(2000) = n'*',--欄位名
@where nvarchar(1000) = n'',--where條件,不需要加where
@orderby nvarchar(500) = n'',--排序條件,不需要加order by
@currentpage int = 1, --當前頁,從1開始,不是0
@pagesize int = 10,--每頁顯示多少條資料
@getcount int =0,--獲取的記錄總數,0則獲取記錄總數,不為0則不獲取
@count int = 0 output--總數)as
begin
--沒有提供排序字段,預設主鍵排序
if @orderby is null or @orderby=''
begin
declare @temptable varchar(200)
--多表聯查如果沒有提供排序字段,自動找第乙個表的主鍵進行排序
if charindex(' on ',@table)>0
set @temptable=substring(@table,0,charindex(' ',@table))
else if charindex(',',@table)>0
begin
set @temptable=substring(@table,0,charindex(',',@table))
--如果有別名如article a,user u
if(charindex(' ',@temptable)>0)
set @temptable=substring(@temptable,0,charindex(' ',@temptable))
endelse
set @temptable=@table--單錶查詢
--查詢表是否存在
if not exists(select * from sysobjects where [name]=@temptable)
begin
raiserror('查詢表%s不存在',12,12,@temptable)
return
end
--查詢排序主鍵
select @orderby=d.name from sysindexes a,sysobjects b,sysindexkeys c,syscolumns d
where c.id = object_id(@temptable) and c.id = b.parent_obj
and a.name = b.name and b.xtype= 'pk ' and a.indid = 1 and d.colid = c.colid and d.id = c.id
--如果沒有主鍵,如檢視
if @orderby is null or @orderby = ''
begin
raiserror('%s必須提供排序字段',12,12,@temptable)
return
endend
--分頁大小
if @pagesize < 1
set @pagesize=10
--預設當前頁
if @currentpage < 1
set @currentpage = 1
--選取字段
if @fields is null or @fields = ''
set @fields='*'
--過濾條件
if @where is null or @where=''
set @where=''
else
set @where=' where '+@where
/*設定分頁引數*/
declare @startrow varchar(50),@endrow varchar(50)
set @startrow = cast(((@currentpage - 1)*@pagesize + 1) as nvarchar(50))
set @endrow = cast(@currentpage*@pagesize as nvarchar(50))
/*執行查詢語句,返回查詢結果*/
exec
('select * from (select row_number() over (order by '+@orderby+') as rownumber,'+@fields+
' from '+@table+@where+') as tempdt where rownumber between '+@startrow+' and '+@endrow)/*
如果@getcount=0,則計算總頁數(這樣設計可以只在第一次計算總頁數,以後呼叫時,
把總頁數傳回給儲存過程,避免再次計算總頁數,當資料量很大時,select count(*)速度也要幾秒鐘)
*/if(@getcount=0)
begin
declare @strsql nvarchar(1200)
set @strsql='select @i=count(*) from '+@table+@where
execute sp_executesql @strsql,n'@i int out',@count out--返回總記錄數
endelse
set @count=@getcount
end
多表查詢的儲存過程分頁
該儲存過程源程式引用 http jiny z.cnblogs.com archive 2006 04 12 373146.html,由於其中多表操作時有乙個bug,做了改進,修改的部分紅色標示,解決了 primarykey table.id 表名.主鍵 時,引數 type無法賦值,進而查詢結果為空的...
MySql 多表聯合查詢分頁儲存過程
分享乙個好用的mysql多表聯合按條件查詢查詢分頁排序儲存過程 procedure structure for ppage drop procedure if exists ppage delimiter create definer root localhost procedure ppage i...
高效多表分頁儲存過程,可支援多表查詢,任意排序
create procedure usp paginglarge tablenames varchar 200 表名,可以是多個表,但不能用別名 primarykey varchar 100 主鍵,可以為空,但 order為空時該值不能為空 fields varchar 4000 要取出的字段,可以...