分頁:要求輸入表名、每頁記錄數、第幾頁,輸出總記錄數、總頁數、記錄內容
Create or replace procedure fenye
(V_table varchar2,V_pagesize number,V_pagenow number) is
V_totalcounts number;
V_totalpages number;
V_sql varchar2(300);
V_pagebegin number;
V_pageend number;
Type tmp_cursor is ref cursor;
V_cursor tmp_cursor;
V_record v_cursor%rowtype; //錯誤因為v_cursor只是個游標類型不是游標或表
V_record emp%rowtype; //此步定義無錯但運行出錯,因為emp中無rn列
Begin
V_pagebegin:=(v_pagenow-1)*v_pagesize+1;
V_pageend:=v_pagenow*v_pagesize;
V_sql:=’select * from (select t1.*,rownum rn from(select * from ‘||v_table||’ ) t1 where rownum<=’||v_pageend||’) where rn>=’||v_pagebegin;
//v_sql會多產生一列rn,這樣fetch v_cursor into v_record不一致報錯
Open v_cursor for v_sql;
Dbms_output.put_line(‘姓名 工種 工資’);
Dbms_output.put_line(‘----------------------------------------’);
loop
Fetch v_cursor into v_record;
Exit when v_cursor%notfound;
Dbms_output.put_line(v_record.ename||’ ’||v_record.job||’ ’||v_record.sal);
End loop;
V_sql:=’select count(*) from ‘||v_table;
Execute immediate v_sql into v_totalcounts;
If mod(v_totalcounts,v_pagesize)=0 then
V_totalpages:=v_totalcounts/v_pagesize;
Else
V_totalpages:=ceil(v_totalcounts/v_pagesize);
End if;
Dbms_output.put_line(‘----------------------------------------’);
Dbms_output.put_line(‘總記錄:’||v_totalcounts||’ 當前頁:’||v_pagenow||’ 總頁數:’||v_totalpages);
Close v_cursor;
End;
以上分页程序报错,是因fetch v_cursor into v_record时v_cursor中的列数与定义的记录型变量v_record不一致
如何修改后正常用???? |
|