Oracle錶值函式的兩種寫法

2021-09-23 16:37:45 字數 4235 閱讀 3872

-- 建立表

declare

cnt integer;

begin

select count(0)

into cnt

from user_all_tables

where table_name = 'cmstemp';

if cnt = 0 then

execute immediate ' create table cmstemp(

id integer primary key,

name varchar2(32),

age integer,

address varchar2(64)

)';end if;

end;

-- 建立 type

declare

cnt integer;

begin

select count(0) into cnt from user_types where type_name = 'cmstemptype';

if cnt = 0 then

execute immediate 'create or replace type cmstemptype as object (

id integer,

name varchar2(32),

age integer,

address varchar2(64)

)'; end if;

end;

-- 建立type 表

declare

cnt integer;

begin

select count(0) into cnt from user_types where type_name = 'cmstemptypetable';

if cnt = 0 then

execute immediate '

create or replace type cmstemptypetable as table of cmstemptype ' ;

end if;

end;

-- 建立臨時表

declare

cnt integer;

begin

select count(0)

into cnt

from user_all_tables

where table_name = 'cmstempglo';

if cnt = 0 then

execute immediate 'create global temporary table cmstempglo(

id integer primary key,

name varchar2(32),

age integer,

address varchar2(64)

) on commit preserve rows';

end if;

end;

--建立包

create or replace package globalpackge

astype cur1 is ref cursor;

end;

第一種方式使用臨時表
create or replace function ns_cms_getcmstem(name varchar2)

return cmstemptypetable

pipelined is

pragma autonomous_transaction;

v_name varchar2(32);

v_age integer;

v_address varchar2(64);

v_id integer;

rws cmstemptype := cmstemptype(null, null, null, null);

v_cur globalpackge.cur1;

begin

execute immediate 'truncate table cmstempglo';

for rec in (select * from cmstemp) loop

v_name := rec.name;

v_age := rec.age;

v_id := rec.id;

v_address := rec.address;

insert into cmstempglo

(id, address, age, name)

values

(v_id, v_address, v_age, v_name);

dbms_output.put_line('11111111111');

end loop;

commit;

open v_cur for

select * from cmstempglo;

loop

fetch v_cur

into rws.id,rws.name,rws.age,rws.address;

exit when v_cur%notfound;

pipe row(rws);

end loop;

close v_cur;

end;

第二種方式不適用臨時表
create or replace function ns_cms_getcmstemp(name varchar2)

return cmstemptypetable

pipelined as

pragma autonomous_transaction;

v_cur globalpackge.cur1;

v_name varchar2(32);

v_address varchar2(64);

v_age integer;

v_id integer;

ret_row cmstemptype := cmstemptype(null, null, null, null);

ret_table cmstemptypetable := cmstemptypetable(); -- 這裡必須這樣寫

begin

for rec in (select * from cmstemp) loop

ret_table.extend;

ret_row.id := rec.id;

ret_row.age := rec.age;

ret_row.name := rec.name;

ret_row.address := rec.address;

ret_table(ret_table.count) := ret_row;

end loop;

commit;

open v_cur for

select * from table(ret_table);

loop

fetch v_cur into ret_row.id,ret_row.name,ret_row.age,ret_row.address; --順序要和type物件裡定義的順序保持一致

exit when v_cur%notfound;

pipe row(ret_row);

end loop;

close v_cur;

end;

插入資料
declare

i integer;

begin

for i in 1 ..100 loop

insert into cmstemp values (i,'zhangsan'||i,1+i,'王府大街'||i);

commit;

end loop;

end;

查詢sql
select * from table(ns_cms_getcmstemp('')) ; 

select * from table(ns_cms_getcmstem('')) ;

flume兩種寫法

1.全寫 bin flume ng agent conf conf name a1 conf file job flume netcat logger.conf dflume.root.logger info,console 2.簡寫 bin flume ng agent n a1 c conf f...

氣泡排序的兩種寫法

public static void main string args function1 a function2 a public static void function1 int a system.out.println system.out.println 第 i 1 次迴圈完成 syste...

全排列的兩種寫法

對於陣列 1,2,3 他們按照從小到大的全排列是 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 現在給你乙個正整數n,n小於8,輸出陣列 1,2,n 的從小到大的全排列。由出口遞迴回溯時,至少返回2層,第一次因為if語句,第二次因為不滿足for迴圈條件 include in...