简体   繁体   English

如何在PostgreSQL中创建表和用户定义的记录?

[英]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.

相关问题 PostgreSQL:用户定义的function性能缓慢 - PostgreSQL: Slow performance of user-defined function SQL用户定义的函数:在用户定义的函数中获取TOP n记录 - SQL User-Defined Functions: Fetching TOP n records in a user-defined function 用户定义的聚合函数,PostgreSQL中有多个输入列 - User-defined aggregate functions with multiple input columns in PostgreSQL PostgreSQL EXISTS 在用户定义的 function 中始终返回 true - PostgreSQL EXISTS in user-defined function always returning true 如何使用新名称创建与现有表类型相同的 SQL 用户定义表类型? - How to create a SQL User-Defined Table Type identical to an existing one with a new name? 如何将 Postgres plpgsql 用户定义的 function 转换为 LANGUAGE SQL 用户定义的 function? - How to convert Postgres plpgsql user-defined function to LANGUAGE SQL user-defined function? 用户定义函数在PostgreSQL中返回表 - User Defined Functions returning Tables in PostgreSQL 为什么CREATE TABLE命令在用户定义的事务中不起作用? - Why is CREATE TABLE command not working in a user-defined transaction? 从用户定义的类创建SQL Server表 - Create SQL Server table from user-defined class 创建用户定义的函数以返回包含客户信息的表? - Create a user-defined function to return a table with customer information?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM