How can I do paging in Sybase without making temp table? (oracle rownum issue)

…衆ロ難τιáo~ 提交于 2019-12-11 04:07:22

问题


In Oracle, usually query like this for paging.

SELECT * FROM (SELECT *, rownum rid  FROM TABLEA WHERE rownum <= #pageend#)
WHERE rid > #pagestart#

However, there is no "rownum" function in Sybase DBMS.

How can I do that query exactlly same in Sybase?

I found some ways.

  1. use "rowcount"

    set rowcount 10

    select * from TABLEA

  2. use identity (make temp table)

    SELECT *, ROWNUM=IDENTITY(8) INTO #TEMP FROM TABLEA

    SELECT * FROM #TEMP WHERE ROWNUM < #pageend# AND ROWNUM >= #pagestart#

    DROP TABLE #TEMP

these are not what I want.

rowcount is set at the session level and i don't want make temp table.


回答1:


If you have a unique id column on your table you could use SELECT TOP n

SELECT TOP 10 *
FROM tableA
WHERE id BETWEEN @start AND @end



回答2:


This will give you all the ids so you can use them in a select like "select * where id_column in (ids from query below)"

select top 10 id_column from trade
where @whereClause
and id_column > 0  //keep replacing this with the max id from the result set
order by id_column


来源:https://stackoverflow.com/questions/1225889/how-can-i-do-paging-in-sybase-without-making-temp-table-oracle-rownum-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!