[英]How to create tables and User-Defined Records in PostgreSQL?
我在Oracle中有下表
create table x(
x_id number,
x_description VARCHAR2(40),
x_date DATE
)
例如,x名稱的PL / SQL表被建模為具有三列的數據庫表,它們分別存儲數字和字符數據以及日期。 盡管您不能使用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;
輸出:
1-豪爾赫-16/12/13
2-何塞-16/12/13
3-安德里亞-16/12/13
4-盧卡斯-16/12/13
如何在PostgreSQL中做到這一點?
pg中不存在AFAIK“表的”,但您可以為其使用臨時表。 您可以使用通常的INSERT / UPDATE / DELETE命令來使用它們。 %rowtype照常對它們起作用。 臨時表僅存在,直到存在會話為止。
另外-您可能知道pg中的“ dbms_output.put_line”是“提高通知”。
加上將其作為匿名塊運行,請使用“ DO $$ ...此處是您的代碼... $$”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.