Sql : 多表联合查询分页
2008-01-03 15:12
447 查看
--功能:通用数据库分页
CREATE PROCEDURE Ture_Page
@PageSize int, --每页的行数
@PageIndex int, --1 代表第一页
@Col varchar(200), --要显示的字段
@Table varchar(200), --所用到的表,复条的话就写from与where之间的内容
@Where varchar(200)='', --所用到的条件
@OKey varchar(50), --排序字段
@Order varchar(20)='ASC' --排序方式
as
declare @cmdstr varchar(2000)
set nocount on
set @cmdstr='select top '
set @cmdstr=@cmdstr+convert(nvarchar,@PageSize)
if @Order='DESC' and @PageIndex>1
set @cmdstr=@cmdstr+' '+@Col+' from '+@Table+' where '+@OKey+'<'
else if @PageIndex=1
set @cmdstr=@cmdstr+' '+@Col+' from '+@Table+' where '+@OKey+'>='
else
set @cmdstr=@cmdstr+' '+@Col+' from '+@Table+' where '+@OKey+'>'
if @PageIndex>1
begin
if @Order='ASC'
set @cmdstr=@cmdstr+'(select max ('+@OKey+') from (select top '
else
set @cmdstr=@cmdstr+'(select min ('+@OKey+') from (select top '
set @cmdstr=@cmdstr+convert(nvarchar,(@PageIndex-1)*@PageSize)
set @cmdstr=@cmdstr+' '+@OKey+' from '+@Table+' order by '+@OKey+' '+@Order+') as t) '
end
else
set @cmdstr=@cmdstr+'0 '
if @Where<>''
set @cmdstr=(@cmdstr+' and '+@Where+' order by '+@OKey+' '+@Order)
else
set @cmdstr=(@cmdstr+'order by '+@OKey+' '+@Order)
print @cmdstr
exec(@cmdstr)
set nocount off
GO
CREATE PROCEDURE Ture_Page
@PageSize int, --每页的行数
@PageIndex int, --1 代表第一页
@Col varchar(200), --要显示的字段
@Table varchar(200), --所用到的表,复条的话就写from与where之间的内容
@Where varchar(200)='', --所用到的条件
@OKey varchar(50), --排序字段
@Order varchar(20)='ASC' --排序方式
as
declare @cmdstr varchar(2000)
set nocount on
set @cmdstr='select top '
set @cmdstr=@cmdstr+convert(nvarchar,@PageSize)
if @Order='DESC' and @PageIndex>1
set @cmdstr=@cmdstr+' '+@Col+' from '+@Table+' where '+@OKey+'<'
else if @PageIndex=1
set @cmdstr=@cmdstr+' '+@Col+' from '+@Table+' where '+@OKey+'>='
else
set @cmdstr=@cmdstr+' '+@Col+' from '+@Table+' where '+@OKey+'>'
if @PageIndex>1
begin
if @Order='ASC'
set @cmdstr=@cmdstr+'(select max ('+@OKey+') from (select top '
else
set @cmdstr=@cmdstr+'(select min ('+@OKey+') from (select top '
set @cmdstr=@cmdstr+convert(nvarchar,(@PageIndex-1)*@PageSize)
set @cmdstr=@cmdstr+' '+@OKey+' from '+@Table+' order by '+@OKey+' '+@Order+') as t) '
end
else
set @cmdstr=@cmdstr+'0 '
if @Where<>''
set @cmdstr=(@cmdstr+' and '+@Where+' order by '+@OKey+' '+@Order)
else
set @cmdstr=(@cmdstr+'order by '+@OKey+' '+@Order)
print @cmdstr
exec(@cmdstr)
set nocount off
GO
相关文章推荐
- 又一个通用分页存储过程,支持表别名,多表联合查询SQL语句
- 又一个通用分页存储过程,支持表别名,多表联合查询SQL语句--转载
- SQL分页存储过程(不支持多表联合查询,不支持多字段排序)
- 又一个通用分页存储过程,支持表别名,多表联合查询SQL语句--转载
- 又一个通用分页存储过程,支持表别名,多表联合查询SQL语句
- 分页联合查询sql 注意事项
- jfinal动态sql管理简洁实现分页多条件模糊查询
- Sql连接查询和联合查询
- 关于SQL 分页的查询
- ORACLE分页查询SQL语法——最高效的分页
- Oracle sql分页查询(高效)
- sql联合两个表查询方法
- ORACLE分页查询SQL语法——最高效的分页
- SQL分页查询(存储过程)
- SQL 联合查询
- SSM框架 Mybatis联合查询 多表查询 分页
- 分页搜索查询sql
- 一个通用的sql 分页查询语句
- SQL 分页支持查询 ROW_NUMBER() OVER (ORDER BY ReportID) AS RowNo
- SQL分页查询优化