创建一个函数,从PostgreSQL中的另一个表插入到表中

时间:2014-02-02 23:20:43

标签: postgresql sql-insert sql-function

我创建了一些类型,然后创建了具有以下类型的表:

Create Type Data_typ AS (
... );

Create Type Communication_typ As (
...;

Create Type CreditCard_typ As (
...);

Create Type Name_typ As (
...);

Create Type Personal_typ As (
...);

Create Type Address_typ As (
...);

Create Type Customers_typ As (
CustomerID integer,
Data Data_typ,
Communication Communication_typ,
CreditCard CreditCard_typ,
Name Name_typ,
Personal Personal_typ,
Address Address_typ);

Create Table Customers_1 of Customers_typ(
primary key (CustomerID));

另外,我还有另一个名为customers的表,其中包含一些数据。我想要做的是创建一个功能,将客户的所有元素复制到customers_1。 customers的列与customers_1相同,但在customers_1上,我创建了包含某些列的类型。

('客户'有20列,我已将其分解为Customers_1上的6种类型。)

这是我的功能:

Create OR REPLACE Function CopyCustomers() RETURNS VOID AS
$$
Begin 
    Insert Into Customers_1(
    Select
    NEW Data_typ (username, password),
    new communication_typ(email,phone),
    new creditCard_typ(creditcardtyp,creditcard,creditcardexpiration),
    new name_typ(firstname,lastname),
    new personal(age,income,gender),
    new address(address1,address2,coty,state,zip)
    From Customers);

End;
$$  LANGUAGE plpgsql;

我不确定new是否正确。我得到的错误是

ERROR:  syntax error at or near "("
LINE 7:  NEW Data_typ (username, password),
                      ^
********** Error **********

ERROR: syntax error at or near "("
SQL state: 42601
Character: 119

更新 - 我尝试使用ROW语法:

Create OR REPLACE Function CopyCustomers() RETURNS VOID AS
$$
Begin 
    Insert Into Customers_1
    Select
    ROW(username, password),
    ROW(email,phone),
    ROW(creditcardtyp,creditcard,creditcardexpiration),
    ROW(firstname,lastname),
    ROW(age,income,gender),
    ROW(address1,address2,coty,state,zip)
    From Customers;

End;
$$  LANGUAGE plpgsql;

我执行了Function,我收到一个错误:

ERROR:  cannot cast type record to data_typ
LINE 4:  ROW(username, "Password"),
         ^
QUERY:  Insert Into Customers_1
    Select
    CustomerID,
    ROW(username, password),
    ROW(email,phone),
    ROW(creditcardcype,creditcard,creditcardexpiration),
    ROW(firstname,lastname),
    ROW(age,income,gender),
    ROW(address1,address2,city,state,zip)
    From CustomerS
CONTEXT:  PL/pgSQL function copycustomers() line 3 at SQL statement
********** Error **********

ERROR: cannot cast type record to data_typ
SQL state: 42846
Context: PL/pgSQL function copycustomers() line 3 at SQL statement

1 个答案:

答案 0 :(得分:2)

一些问题。

Insert Into Customers_1(
Select

你不需要那里的人,只是

INSERT INTO customers_1 SELECT ....

至于此:

NEW Data_typ (username, password),

您是否只是想创建Data_typ,其中Data_typ是使用CREATE TYPE Data_typ AS (...)创建的复合类型?

如果是这样,你想要:

ROW(username, password)::xy

示例,给定CREATE TYPE xy AS (x integer, y integer);

regress=> SELECT new xy(1,2);
ERROR:  syntax error at or near "("
LINE 1: SELECT new xy(1,2);
                     ^
regress=> SELECT xy(1,2);
ERROR:  function xy(integer, integer) does not exist
LINE 1: SELECT xy(1,2);
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
regress=> SELECT ROW(1,2)::xy;
  row  
-------
 (1,2)
(1 row)