key words :Oracle分頁 視圖
google了一下關(guān)于Oracle的分頁方法,方法還不少,大多數(shù)效果差不多-有點(diǎn)惡心. 惡心也要作,不過后來就是大家都用得這種方式在我這里出現(xiàn)了新問題,奇怪的是怎么沒有別人碰到?
String condition?=?"?teacher_id?=?"?+?userId?+?"?and?school_id="+siteId;
sql?=
??? "?select * "?+
??? "?from your_table where?"?+?condition?+
??? "?and?rowid?not?in?(?select?rowid?from your_table where"?+?condition?+
??? "?and?rownum?<=?"?+?(pageIndex?-?1)?*?Constants.PAGE_NUMS +?")?"?+
??? "?and?rownum?<=?"?+?Constants.PAGE_NUMS ;
現(xiàn)在的問題是我需要按照table的某個(gè)字段排序,于是改成如下:
String?condition?=?"?teacher_id?=?"?+?userId?+?"?and?school_id="+siteId;
sql?=
????"?select?*?"?+
????"?from?your_table?where?"?+?condition?+
????"?and?rowid?not?in?(?select?rowid?from?your_table?where"?+?condition?+
????"?and?rownum?<=?"?+?(pageIndex?-?1)?*?Constants.PAGE_NUMS?+?"order by id desc)?"?+
????"?and?rownum?<=?"?+?Constants.PAGE_NUMS + " order by id desc";
這個(gè)sql有問題么?
答案是可能有問題,也可能沒有問題,因?yàn)閾?jù)說在8i的Oracle版本之前都不行,實(shí)際上也不盡然,在我的9i和10g我得到的是同樣的錯(cuò)誤 "
missing right parenthesis",還有一位兄弟估計(jì)是DBA建議我去metalink打一個(gè)patch,埃,動(dòng)作太大了,不敢動(dòng)。
問題還是要解決,試了下類似于select a.*,rownum r from (select * from table where ...) a where rownum < 10 等的方法,效果一樣,就是不能加嵌套的order by
最后,用視圖的方法間接解決問題,因?yàn)槲乙鉀Q的問題實(shí)際就是按某個(gè)字段排序,那么在視圖里先對(duì)table進(jìn)行排序,再在視圖的基礎(chǔ)上作操作就OK了.
另,還有一種不錯(cuò)的實(shí)現(xiàn)方法,即用OracleCachedRowSet,分頁也比較簡(jiǎn)單,有點(diǎn)類似于hibernate,由于時(shí)間關(guān)系沒有時(shí)間去看,感興趣的朋友可以看一下.
BTW: 對(duì)于視圖可能rowid有問題,可以改成視圖的某個(gè)主鍵替換
String?condition?=?"?teacher_id?=?"?+?userId?+?"?and?school_id="+siteId;
sql?=
????"?select?*?"?+
????"?from?your_table?where?"?+?condition?+
????"?and?id?not?in?(?select?id?from?your_table?where"?+?condition?+
????"?and?rownum?<=?"?+?(pageIndex?-?1)?*?Constants.PAGE_NUMS?+?"order?by?id?desc)?"?+
????"?and?rownum?<=?"?+?Constants.PAGE_NUMS?+?"?order?by?id?desc";