在PL SQL中自定義陣列型別

2021-07-10 02:25:51 字數 2229 閱讀 4474

知識準備

-定長陣列

--定長數字型陣列,長度為10

type identifer is varray(10) of number;

如:

set serveroutput on;

declare

type v_array is varray(10) of number;

v_index number;

v_arr v_array;

v_arr := v_array(1,23,567,233,66);

begin

forindex

in1..v_arr.count

loop

dbms_output.put_line(v_arr(v_index));

endloop;

end;

/

-變長陣列

--可變長數字型陣列,陣列中元素長度為10,角標索引為integer自動增長

type identifer is table of number(10) index

by binary_integer;

如:

set serveroutput on;

declare

type v_array is

is table of number(10) index

by binary_integer;

v_index number;

v_arr v_array;

v_arr := v_array(1,23,567,233,66);

begin

forindex

in1..v_arr.count

loop

dbms_output.put_line(v_arr(v_index));

endloop;

end;

/

實戰
--為了在儲存過程中可以使用自定義的陣列型別,此處使用程式包的方式

create

or replace package useronline_pkg

astype int_array is table of number(10) index

by binary_integer;

procedure

batch_updateuseronline

(v_userid in int_array, v_onlines in int_array);

end useronline_pkg;

/--宣告程式包體

create

or replace package body useronline_pkg

asprocedure

batch_updateuseronline

(v_userid in int_array, v_onlines in int_array)

asuser_id

number

(10);

user_online number(1);

v_index number(8);

begin

for v_index in

1..v_userid.count

loop

user_id := v_userid(v_index);

user_online := v_onlines(v_index);

merge into useronline tb

using (select count(userid) as cnt from useronline where userid=user_id) tb_tmp

on(tb_tmp.cnt <> 0)

when matched then

update set tb.onlines=user_online, tb.updatetime=sysdate where userid=user_id

when not matched then

insert values(user_id, user_online, sysdate);

commit;

endloop;

end batch_updateuseronline;

end useronline_pkg;

/

這裡是我的處理方法,如有更好的方法,歡迎指教。

plsql 開發手記 之自定義型別

在plsql的開發中 我們經常會使用到自己自定義的 型別 create or replace type enumber as table of number 這樣 enumber 就可以在 其他儲存過程,或者函式中來使用 create or replace type split type is ta...

scala陣列,自定義型別,自定義排序等

定義陣列 val active array.fill numruns true val costs array.fill numruns 0.0 var activeruns new arraybuffer int 0 until numruns 自定義型別 type weightedpoint v...

物件型別陣列 自定義排序

我們可以對任何物件型別的陣列排序,比如,物件person有名字和年齡屬性,我們希望根據年齡排序,那麼我們可以這麼寫 const friends function compareperson property if a property b property return 0 console.log ...