在網上搜到如下存儲過程分頁的代碼:













1.修改SQL,將select和from做標記,替換成selects和froms












public String parseProcSql(String sql){
String stemp = sql.toUpperCase();
String v[] = stemp.split("UNION");
String result = "";
for(int i=0; i<v.length; i++){
int tmp = 0;
int from = 0;
tmp = v[i].indexOf("SELECT",0);
if(tmp>0){
v[i] = v[i].substring(0,tmp) + "SELECTS" + v[i].substring(tmp+6);
}else{
v[i] = "SELECTS" + v[i].substring(6);
}
if(i==0){
while(tmp!=-1){
from = v[i].indexOf("FROM",from+1);
tmp = v[i].indexOf("SELECT",tmp+1);
if(tmp>from)break;
}
result = v[i].substring(0,from) + "FROMS" + v[i].substring(from+4);
}else{
result = result + "UNION" + v[i];
}
}
return result;
}
3.使用union all分頁仍然會有問題.String stemp = sql.toUpperCase();
String v[] = stemp.split("UNION");
String result = "";
for(int i=0; i<v.length; i++){
int tmp = 0;
int from = 0;
tmp = v[i].indexOf("SELECT",0);
if(tmp>0){
v[i] = v[i].substring(0,tmp) + "SELECTS" + v[i].substring(tmp+6);
}else{
v[i] = "SELECTS" + v[i].substring(6);
}
if(i==0){
while(tmp!=-1){
from = v[i].indexOf("FROM",from+1);
tmp = v[i].indexOf("SELECT",tmp+1);
if(tmp>from)break;
}
result = v[i].substring(0,from) + "FROMS" + v[i].substring(from+4);
}else{
result = result + "UNION" + v[i];
}
}
return result;
}
posted @ 2009-07-21 13:48 Yuanhua 閱讀(499) | 評論 (0) | 編輯 收藏