I have the following table in 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. Although you cannot use SQL statements to manipulate a PL/SQL table, its primary key gives you array-like access to rows.
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
2 - Jose - 13/12/16
3 - Andrea - 13/12/16
4 - Lucas - 13/12/16
How can I do this in PostgreSQL?
AFAIK "table of" does not exist in pg but you can use temporary tables for it. You work with them with usual INSERT/ UPDATE/ DELETE commands. Also %rowtype works on them as usual. Temp table lives only untill session exists.
Plus - you probably know "dbms_output.put_line" is "raise NOTICE" in pg.
Plus to run it as anonymous block use "DO $$ ...here your code ... $$"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.