声明了一个类型对象
 create TYPE  type1 as (
No numeric(10,0),
name text,
note text)obj1 type1 第一次赋初值
INSERT INTO obj1 VALUES (0,‘老瓦’,‘’);执行的时候,提示obj1 是符合类型。我理解obj1是一个bean,但是似乎是list.请问如何声明一个bean,并对其赋值,读取。

解决方案 »

  1.   

    postgrelsq help:
    这个例子创建一个复合类型并且在一个函数定义中使用它: 
    CREATE TYPE compfoo AS (f1 int, f2 text);CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS $$
      SELECT fooid, fooname FROM foo
    $$ LANGUAGE SQL;
      

  2.   

    用row表达式
    INSERT INTO obj1 VALUES row(0,‘老瓦’,‘’);
      

  3.   

    Row Constructors
    A row constructor is an expression that builds a row value (also called a composite value) using values for its member fields. A row constructor consists of the key word ROW, a left parenthesis, zero or more expressions (separated by commas) for the row field values, and finally a right parenthesis. For example: SELECT ROW(1,2.5,'this is a test');The key word ROW is optional when there is more than one expression in the list. A row constructor can include the syntax rowvalue.*, which will be expanded to a list of the elements of the row value, just as occurs when the .* syntax is used at the top level of a SELECT list. For example, if table t has columns f1 and f2, these are the same: SELECT ROW(t.*, 42) FROM t;
    SELECT ROW(t.f1, t.f2, 42) FROM t;
    Note: Before PostgreSQL 8.2, the .* syntax was not expanded, so that writing ROW(t.*, 42) created a two-field row whose first field was another row value. The new behavior is usually more useful. If you need the old behavior of nested row values, write the inner row value without .*, for instance ROW(t, 42). By default, the value created by a ROW expression is of an anonymous record type. If necessary, it can be cast to a named composite type — either the row type of a table, or a composite type created with CREATE TYPE AS. An explicit cast might be needed to avoid ambiguity. For example: CREATE TABLE mytable(f1 int, f2 float, f3 text);CREATE FUNCTION getf1(mytable) RETURNS int AS 'SELECT $1.f1' LANGUAGE SQL;-- No cast needed since only one getf1() exists
    SELECT getf1(ROW(1,2.5,'this is a test'));
     getf1
    -------
         1
    (1 row)CREATE TYPE myrowtype AS (f1 int, f2 text, f3 numeric);CREATE FUNCTION getf1(myrowtype) RETURNS int AS 'SELECT $1.f1' LANGUAGE SQL;-- Now we need a cast to indicate which function to call:
    SELECT getf1(ROW(1,2.5,'this is a test'));
    ERROR:  function getf1(record) is not uniqueSELECT getf1(ROW(1,2.5,'this is a test')::mytable);
     getf1
    -------
         1
    (1 row)SELECT getf1(CAST(ROW(11,'this is a test',2.5) AS myrowtype));
     getf1
    -------
        11
    (1 row)
      

  4.   

    理解这里的type是一个table。类似oracle中的
    TYPE Name_TBL IS TABLE OF Type1 INDEX BY BINARY_INTEGER;那么oracle中的type,在pgsql是如何对应的呢?