該儲存過程源程式引用
http://jiny-z.cnblogs.com/archive/2006/04/12/373146.html,由於其中多表操作時有乙個bug,做了改進,修改的部分紅色標示,解決了@primarykey = 'table.id'(表名.主鍵)時,引數@type無法賦值,進而查詢結果為空的問題。
修改後的**如下:
create procedure up_getrecordbypage
@tablenames varchar(500), --表名,可以是多個表,但不能用別名
@primarykey varchar(100), --主鍵,可以為空,但@order為空時該值不能為空
@fields varchar(500), --要取出的字段,可以是多個表的字段,可以為空,為空表示select *
@pagesize int, --每頁記錄數
@currentpage int, --當前頁,0表示第1頁
@filter varchar(500) = '', --條件,可以為空,不用填 where
@group varchar(500) = '', --分組依據,可以為空,不用填 group by
@order varchar(500) = '' --排序,可以為空,為空缺省按主鍵公升序排列,不用填 order by
asbegin
declare @sortcolumn varchar(500)
declare @operator char(2)
declare @sorttable varchar(500)
declare @sortname varchar(500)
if @fields = ''
set @fields = '*'
if @filter = ''
set @filter = 'where 1=1'
else
set @filter = 'where ' + @filter
if @group <> ''
set @group = 'group by ' + @group
if @order <> ''
begin
declare @pos1 int, @pos2 int
set @order = upper(@order)
if charindex(' desc', @order) > 0
if charindex(' asc', @order) > 0
begin
if charindex(' desc', @order) < charindex(' asc', @order)
set @operator = '<='
else
set @operator = '>='
endelse
set @operator = '<='
else
set @operator = '>='
set @sortcolumn = replace(replace(replace(@order, ' asc', ''), ' desc', ''), ' ', '')
set @pos1 = charindex(',', @sortcolumn)
if @pos1 > 0
set @sortcolumn = substring(@sortcolumn, 1, @pos1-1)
set @pos2 = charindex('.', @sortcolumn)
if @pos2 > 0
begin
set @sorttable = substring(@sortcolumn, 1, @pos2-1)
if @pos1 > 0
set @sortname = substring(@sortcolumn, @pos2+1, @pos1-@pos2-1)
else
set @sortname = substring(@sortcolumn, @pos2+1, len(@sortcolumn)-@pos2)
endelse
begin
set @sorttable = @tablenames
set @sortname = @sortcolumn
endend
else
begin
set @sorttable = @tablenames
set @sortcolumn = @primarykey
set @sortname = @sortcolumn
set @pos1 = charindex('.', @sortname)
if @pos1 > 0
begin
set @sorttable = substring(@sortname, 1, @pos1-1)
set @sortname = substring(@sortname, @pos1 + 1, len(@sortname))
endset @order = @sortcolumn
set @operator = '>='
enddeclare @type varchar(50)
declare @prec int
select @type=t.name, @prec=c.prec
from sysobjects o
join syscolumns c on o.id=c.id
join systypes t on c.xusertype=t.xusertype
where o.name = @sorttable and c.name = @sortname
if charindex('char', @type) > 0
set @type = @type + '(' + cast(@prec as varchar) + ')'
declare @toprows int
set @toprows = @pagesize * @currentpage + 1
declare @strsql nvarchar(2000)
set @strsql = 'declare @sortcolumnbegin ' + cast(@type as varchar) + '
set rowcount ' + cast(@toprows as varchar) +'
select @sortcolumnbegin=' + @sortcolumn + ' from ' + @tablenames + ' ' + @filter + ' ' + @group + ' order by ' + @order + '
set rowcount ' + cast(@pagesize as varchar) + '
select ' + @fields + ' from ' + @tablenames + ' ' + @filter + ' and ' + @sortcolumn + ' ' + @operator + '@sortcolumnbegin ' + @group + ' order by ' + @order + '
'exec(@strsql)
print(@strsql)
endgo
多表查詢分頁儲存過程
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.realn...
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 要取出的字段,可以...