您好,登錄后才能下訂單哦!
PostgreSQL可用ARRAY來(lái)替代Oracle中的collection type,包括associative array/Varrays (Variable-Size Arrays)/Nested Tables
Oracle
簡(jiǎn)單舉個(gè)例子:
drop table if exists employee;
create table employee(id int,name varchar(30),department varchar(30),salary float);
insert into employee(id,name,department,salary) select rownum,substrb(object_name,1,30),substrb(object_name,1,30),1000 from dba_objects;
DECLARE
TYPE EmpTabTyp IS TABLE OF employee%ROWTYPE
INDEX BY PLS_INTEGER;
emp_tab EmpTabTyp;
i int := 0;
BEGIN
/* Retrieve employee record. */
for c1 in (select * from employee) loop
emp_tab(i).id := c1.id;
emp_tab(i).name := c1.name;
emp_tab(i).department := c1.department;
emp_tab(i).salary := c1.salary;
i := i+1;
end loop;
-- SELECT * INTO emp_tab(100) FROM employee WHERE id = 100;
END;
/
更簡(jiǎn)單的做法是使用bulk collection
DECLARE
TYPE EmpTabTyp IS TABLE OF employee%ROWTYPE
INDEX BY PLS_INTEGER;
emp_tab EmpTabTyp;
i int := 0;
BEGIN
/* Retrieve employee record. */
select id,name,department,salary bulk collect into emp_tab from employee;
END;
/
PostgreSQL
使用ARRAY
drop type record_of_employee;
CREATE TYPE record_of_employee AS (id int,name varchar(30),department varchar(30),salary float);
do
$$
declare
employees record_of_employee[];
begin
select array_agg(employee) into employees from employee limit 1;
raise notice 'id is %',employees[1].id;
raise notice 'name is %',employees[1].name;
end
$$;
對(duì)于Associative array indexed by string,PG的數(shù)組則替代不了.
DECLARE
-- Associative array indexed by string:
TYPE population IS TABLE OF NUMBER -- Associative array type
INDEX BY VARCHAR2(64); -- indexed by string
...
參考資料
PL/SQL Collections and Records
Oracle PL/SQL Collections: Varrays, Nested & Index by Tables
Collections in Oracle PL/SQL
Working with Collections
Take a Dip into PostgreSQL Arrays
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。