返回顶部
首页 > 资讯 > 数据库 >Oracle vs PostgreSQL Develop(17) - ARRAY
  • 343
分享到

Oracle vs PostgreSQL Develop(17) - ARRAY

2024-04-02 19:04:59 343人浏览 安东尼
摘要

postgresql可用ARRAY

postgresql可用ARRAY来替代oracle中的collection type,包括associative array/Varrays (Variable-Size Arrays)/Nested Tables

Oracle
简单举个例子:


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
   
   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;
/

更简单的做法是使用bulk collection


DECLARE
   TYPE EmpTabTyp IS TABLE OF employee%ROWTYPE
      INDEX BY PLS_INTEGER;
   emp_tab EmpTabTyp;
   i int := 0;
BEGIN
   
   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
$$;

对于Associative array indexed by string,PG的数组则替代不了.


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

您可能感兴趣的文档:

--结束END--

本文标题: Oracle vs PostgreSQL Develop(17) - ARRAY

本文链接: https://lsjlt.com/news/48838.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作