SQL與Oracle兩種形式分頁儲存過程

2021-04-22 04:27:06 字數 4474 閱讀 6693

sql分頁儲存過程

///create procedure [dbo].[sp_pageindex]

(-- add the parameters for the stored procedure here

@tblname varchar(50),         --(表名、檢視名)

@strgetfields varchar(255)='*', --(查詢字段列表)

@ordername varchar(255),         --(排序欄位名稱)

@maxrows int ,                 --(每頁顯示記錄數)

@startindex int,                 --(當前頁碼)

@iscount bit=0,                 --是否返回查詢總記錄數 (0: no;1:yes)

@ordertype bit=0,               --排序型別名稱(0:公升序;1:降序)

@strwhere varchar(3000)=''      --查詢條件(where部分,不用加where關鍵字))as

begin

-- set nocount on added to prevent extra result sets from

-- interfering with select statements.

set nocount on;

-- insert statements for procedure here

declare @strsql varchar(5000)

declare @strtemp varchar(100)

declare @strorder varchar(100)

declare @strcount varchar(3000) -- 返回資料的總條數語句

if @ordertype!=0

begin

set @strtemp='<(select min'

set @strorder='order by ['+@ordername+'] desc'

endelse

begin

set @strtemp='>(select max'

set @strorder='order by ['+@ordername+'] asc'

endif @startindex=1

begin

if @strwhere!=''

begin

set @strsql='select top '+str(@maxrows)+' '+@strgetfields+' from ['+@tblname+']where '+@strwhere+' '+@strorder

set @strcount = 'select count(*) as total from ' + @tblname + ' as count where ' + @strwhere

endelse

begin

set @strsql='select top '+str(@maxrows)+' '+@strgetfields+' from ['+@tblname+'] '+@strorder

set @strcount = 'select count(*) as total from ' + @tblname

endend

else

begin

if @strwhere!=''

begin

set @strsql='select top '+str(@maxrows)+' '+@strgetfields+' from ['+@tblname+'] where ['

+@ordername+'] '+@strtemp+'(['+@ordername+']) from (select top '+str((@startindex-1)*@maxrows)

+' ['+@ordername+'] from ['+@tblname+'] where '+@strwhere+' '+@strorder+') as tabletemp) and '

+@strwhere+' '+@strorder

set @strcount = 'select count(*) as total from ' + @tblname + ' as count where ' + @strwhere

endelse

begin

set @strsql='select top '+str(@maxrows)+' '+@strgetfields+' from ['+@tblname+'] where ['+@ordername+'] '+@strtemp+'(['+@ordername+']) from (select top '+str((@startindex-1)*@maxrows)

+' ['+@ordername+'] from ['+@tblname+']'+@strorder+') as tabletemp) '+@strorder

set @strcount = 'select count(*) as total from ' + @tblname

endend

if @iscount!=0 ----按需要返回資料的總條數

begin

exec (@strcount)

endexec (@strsql)

end/

oracle儲存過程分頁(包和包體結構)

***************包結構*******************

create or replace package pg_bk_querypage

is--定義游標

type bkcursor is ref cursor;

procedure sp_pageindex

(pi_tbname in varchar2, --表名

pi_field0 in varchar2, --欄位集

pi_rowfil in varchar2, --過濾條件

pi_sortst in varchar2, --排序集

pi_pagenu in varchar2, --頁碼

pi_pagepg in varchar2, --每頁顯示條數

p0_totalc out number, --總記錄數

p0_cursor out bkcursor --返回游標

);end pg_bk_querypage;

***************包體結構*******************

create or replace package body pg_bk_querypage

isprocedure  sp_pageindex

(pi_tbname in varchar2, --表名

pi_field0 in varchar2, --欄位集

pi_rowfil in varchar2, --過濾條件

pi_sortst in varchar2, --排序集

pi_pagenu in varchar2, --頁碼

pi_pagepg in varchar2, --每頁顯示條數

p0_totalc out number, --總記錄數

p0_cursor out bkcursor --返回游標)is

v_table1 varchar2(500); --動態名1

v_table2 varchar2(500); --動態名2

v_table3 varchar2(500); --動態名3

v_table4 varchar2(500); --動態名4

v_pagenu varchar2(10); --頁碼變數

v_pagesy varchar2(10); --記錄數數量

begin

v_pagenu:=pi_pagenu*pi_pagepg;

v_pagesy:=(pi_pagenu-1)*pi_pagepg;

v_table1 := 'select t1.'|| pi_field0 ||', rownum as rn from '|| pi_tbname || ' t1';

v_table2 := v_table1 ||' where ' || pi_rowfil || ' and  rownum <= '|| v_pagenu  ;

v_table3 := 'select count(*) as sum from ' || pi_tbname || ' where ' || pi_rowfil;

v_table4 := 'select * from ('|| v_table2 ||') t where t.rn>'||v_pagesy || 'order by '||pi_sortst||' desc';

execute immediate v_table3 into p0_totalc; --返回總記錄

open p0_cursor for v_table4;

end;

end pg_bk_querypage;

Oracle的分頁查詢兩種形式

oracle分頁查詢語句使我們最常用的語句之一,下面就為您介紹的oracle分頁查詢語句的用法,如果您對此方面感興趣的話,不妨一看。oracle分頁查詢語句基本上可以按照本文給出的格式來進行套用。oracle分分頁查詢格式 按 ctrl c 複製 select from select a.rownu...

樹上差分的兩種實現形式

簡單理解樹上差分 點,邊 關於樹上差分,我在這推薦一道題目poj 3417 題目翻譯 樹上差分用來解決點的覆蓋或者邊的覆蓋的問題,需分別運用點的差分或者邊的差分。先從例題入手 給定點數為n,邊數為n 1的樹 無向邊且無重邊 先有m條連線a,b的非樹邊,求每條 樹邊 被 非樹邊 覆蓋了多少次?邊的差分...

oracle 客戶端,分兩種

oracle 客戶端,分兩種 第一種,安裝提供客戶端,對於windows系統 1.把 oracle home lib加到path環境變數.2.把 oracle home jdbclibclasses12.jar加到classpath環境變數裡.也可以把classes12.jar拷貝到tomcat的c...