oracle自定義聚集函式

2021-07-08 18:59:02 字數 3869 閱讀 8944

oracle自定義聚集函式採用oracle提供的介面,是乙個固定的模式。要定義oracle聚集函式需要定義一下4個函式。

a:static function odciaggregateinitialize(sctx in out str_conn_type )

return number

自定義聚集函式初始化設定,從這兒開始乙個聚集函式。

b:member function odciaggregateiterate(self in out str_conn_type ,value in number )

return number

自定義聚集函式最主要的步驟,這個函式定義我們的聚集函式具體做什麼操作,後面的例子。是取最大值、最小值、平均值、還是做連線操作。self為當前聚集函式的指標,用來與前面的計算結果進行關聯。

c:member function odciaggregatemerge(self in out str_conn_type,ctx2 in str_conn_type)

return number,

用來合併兩個聚集函式的兩個不同的指標對應的結果,使用者合併不同結果結的資料,特別是處理並行(parallel)查詢聚集函式的時候。

d:member function odciaggregateterminate(self in str_conn_type,returnvalue out varchar2,flags in number)

終止聚集函式的處理,返回聚集函式處理的結果。

例子:

create

orreplace

type

str_conn_type

asobject (

vstr

varchar2

(4000

),static

function

odciaggregateinitialize(sctx

inout

str_conn_type )

return

number

,member

function

odciaggregateiterate(

self

inout

str_conn_type ,

value

innumber

)return

number

,member

function

odciaggregatemerge(

self

inout

str_conn_type,

ctx2

instr_conn_type)

return

number

,member

function

odciaggregateterminate(

self

instr_conn_type,

returnvalue

outvarchar2

,flags

innumber

)return

number

); /

create

orreplace

type

body

str_conn_type

isstatic

function

odciaggregateinitialize(sctx

inout

str_conn_type)

return

number

isbegin

sctx := str_conn_type(

null

);return

odciconst.success;

end;

member

function

odciaggregateiterate(

self

inout

str_conn_type,

value

innumber

)return

number

isbegin

self.vstr := self.vstr ||

';'||

value

;return

odciconst.success;

end;

member

function

odciaggregatemerge(

self

inout

str_conn_type,

ctx2

instr_conn_type)

return

number

isbegin

return

odciconst.success;

end;

member

function

odciaggregateterminate(

self

instr_conn_type,

returnvalue

outvarchar2

,flags

innumber

)return

number

isbegin

returnvalue :=

ltrim

(self.vstr,

';');

return

odciconst.success;

end;

end; /

create

orreplace

function

connstr(input

varchar2

)return

varchar2

parallel_enable

aggregate

using

str_conn_type; /

應用

select

*from

t_ssq_bqhm t

order

byqh,hmlx,hm

qh hm hmlx

1 05021 1 0

2 05021 2 0

3 05021 3 0

4 05021 4 0

5 05021 5 0

6 05021 6 0

7 05021 7 0

8 05021 8 0

9 05021 9 0

10 05021 1 1

11 05021 12 1

12 05028 2 0

13 05028 6 0

14 05028 7 0

15 05028 8 0

16 05028 18 0

17 05028 19 0

18 05028 20 0

19 05028 23 0

20 05028 28 0

21 05028 29 0

22 05028 30 0

23 05028 2 1

24 05028 9 1

25 05028 10 1

26 05028 11 1

select

qh,hmlx,connstr(hm)

ashms

from

t_ssq_bqhm t

group

byqh,hmlx

qh hmlx hms

1 05021 0 1;2;4;3;5;7;9;8;6

2 05021 1 1;12

3 05028 0 2;8;6;19;30;29;28;23;20;18;7

4 05028 1 2;11;9;10

**:

Oracle自定義函式

語法如下 create or replace function function name argment type,argment type return return type 返回資料的型別 變數的申明,比如 stryuan varchar2 150 begin function body 函...

oracle 自定義函式

下面是乙個前輩寫的判斷是否是手機號的函式 create or replace function ismobile pmsg varchar2 return number isvprefix varchar2 20 vlen number begin vlen lengthb pmsg if vlen...

Oracle自定義函式

二 刪除自定義函式 三 應用經驗 在oracle資料庫中,為了實現特定的功能,可以自定義函式,就像c c 語言,除了系統的庫函式,程式設計師還會編寫很多自定義的函式。create or replace function 函式名 引數1 模式 資料型別,return 資料型別 as 定義區域性變數。變...