数据仓库服务 GAUSSDB(DWS)-复合类型:复合类型的声明

时间:2024-12-24 10:32:44

复合类型的声明

GaussDB (DWS)支持用户使用CREATE TYPE定义复合类型:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
CREATE TYPE complex AS (
        r       double precision,
        i       double precision
    );

CREATE TYPE inventory_item AS (
        name            text,
        supplier_id     integer,
        price           numeric
    );

定义复合类型之后,可用来创建表或函数:

1
2
3
4
5
6
CREATE TABLE on_hand (
        item      inventory_item,
        count     integer
    );

INSERT INTO on_hand VALUES (ROW('fuzzy dice', 42, 1.99), 1000);
1
2
3
4
CREATE FUNCTION price_extension(inventory_item, integer) RETURNS numeric
    AS 'SELECT $1.price * $2' LANGUAGE SQL;

SELECT price_extension(item, 10) FROM on_hand;
support.huaweicloud.com/sqlreference-dws/dws_06_0371.html