[英]How to create tables and User-Defined Records in PostgreSQL?
I have the following table in Oracle 我在Oracle中有下表
create table x(
x_id number,
x_description VARCHAR2(40),
x_date DATE
)
For example, a PL/SQL table of x names is modeled as a database table with three columns, which store a number and character data and date respectively. 例如,x名称的PL / SQL表被建模为具有三列的数据库表,它们分别存储数字和字符数据以及日期。 Although you cannot use SQL statements to manipulate a PL/SQL table, its primary key gives you array-like access to rows.
尽管您不能使用SQL语句来操作PL / SQL表,但是它的主键使您可以像数组一样访问行。
declare
type tab_x is table of x%rowtype;
row x%rowtype;
list tab_x;
begin
begin
list.delete;
exception
when collection_is_null then
list := tab_x();
end;
row.observacion := 'Jorge';
row.numero := '1';
row.fch_ins := sysdate;
list.extend;
list(list.last) := row;
row.observacion := 'Andrea';
row.numero := '2';
row.fch_ins := sysdate;
list.extend;
list(list.last) := row;
row.observacion := 'Jose';
row.numero := '3';
row.fch_ins := sysdate;
list.extend;
list(list.last) := row;
row.observacion := 'Lucas';
row.numero := '4';
row.fch_ins := sysdate;
list.extend;
list(list.last) := row;
for i in list.first .. list.last loop
row := list(i);
dbms_output.put_line(row.x_id ||' - '|| row.x_description ||' - '|| row.x_date);
end loop;
end;
Output: 输出:
1 - Jorge - 13/12/16 1-豪尔赫-16/12/13
2 - Jose - 13/12/16 2-何塞-16/12/13
3 - Andrea - 13/12/16 3-安德里亚-16/12/13
4 - Lucas - 13/12/16 4-卢卡斯-16/12/13
How can I do this in PostgreSQL? 如何在PostgreSQL中做到这一点?
AFAIK "table of" does not exist in pg but you can use temporary tables for it. pg中不存在AFAIK“表的”,但您可以为其使用临时表。 You work with them with usual INSERT/ UPDATE/ DELETE commands.
您可以使用通常的INSERT / UPDATE / DELETE命令来使用它们。 Also %rowtype works on them as usual.
%rowtype照常对它们起作用。 Temp table lives only untill session exists.
临时表仅存在,直到存在会话为止。
Plus - you probably know "dbms_output.put_line" is "raise NOTICE" in pg. 另外-您可能知道pg中的“ dbms_output.put_line”是“提高通知”。
Plus to run it as anonymous block use "DO $$ ...here your code ... $$" 加上将其作为匿名块运行,请使用“ DO $$ ...此处是您的代码... $$”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.