第一次涉及使用到sybase数据库,需要通过sql分页查询某个表数据。sybase无法直接分页,只提供top方法获取top几的数据,通过网上比较常见的方法是通过建立临时表的方式获取(效率有待确定)。
1.将需要分页的表存定义rowid存进临时表:
select top tabelNum rowid=identity(12), * into #tmp from tableName
2.将分页起始位置作为参数进行查询:
select * from #tmp
where rowid between startNum and endNum
3.删除临时表:
drop table #tmp
注意:sybase创建的临时表默认存放在tempdb库中,保险起见,在进行临时创建时提前判断表是否存在:
select count(*) from tempdb..sysobjects
where id=object_id('tempdb..#tmp')
如果存在进行删除:(当表不存在时会报错)
if exists (select count(*) from tempdb..sysobjects where id=object_id('tempdb..#tmp')) drop table #tmp
可行方法:
if object_id('tempdb.dbo.#tmp') is not null
drop table #tmp
注:判断普通表是否存在:(不适用于临时表)
if exists (select 1
from sysobjects
where id = object_id('tableName')
and type = 'U')
drop table tableName