如何从plpgsql函数获取结果

时间:2019-07-30 23:50:16

标签: postgresql plpgsql

我有一个函数返回定义好的结果集

CREATE TABLE tbl_obj_clients (
    id uuid NOT NULL,
    "type" schappnme_300.enm_obj_clients_types NOT NULL,
    status schappnme_300.enm_obj_clients_statuses NOT NULL,
    info jsonb NOT NULL,
    CONSTRAINT pk_tbl_obj_clients PRIMARY KEY (id)
);

但是当我从代码中调用函数时,返回类型被转换为RECORD类型,而不是众所周知的tbl_obj_clients

CREATE OR REPLACE FUNCTION schappnme_300.fn_create_clients(in_type text DEFAULT 'ordinary'::text, in_status text DEFAULT 'active'::text, in_info json DEFAULT NULL::json, in_debug boolean DEFAULT false)
    RETURNS SETOF schappnme_300.tbl_obj_clients
    LANGUAGE plpgsql
    AS $function$
    ...

实际上,当我调用特定功能时

v_client tbl_obj_clients := NULL;

FOR v_client IN SELECT fn_create_clients('system', 'active', v_com_json, in_debug)
LOOP
    RAISE NOTICE '%',v_client;
END LOOP;

我获得了具体的输出

("(d8526882-f656-4873-9b49-de972202ca66,system,active,""{""""data"""": [{""""type"""": """"string"""", """"label"""": """"Name"""", """"value"""": """"NotifyMe"""", """"description"""": """"obj_client_name"""", """"placeholder"""": """"[*client_name*]"""", """"sequenceorder"""": 10}, {""""type"""": """"string"""", """"label"""": """"Description"""", """"value"""": """"NotifyMe Administration Account"""", """"description"""": """"obj_client_description"""", """"placeholder"""": """"[*client_description*]"""", """"sequenceorder"""": 20}, {""""type"""": """"datetime"""", """"label"""": """"Created On"""", """"value"""": """"2019-07-29 17:30:00"""", """"description"""": """"obj_client_createdon"""", """"placeholder"""": """"[*client_created_on*]"""", """"sequenceorder"""": 30}, {""""type"""": """"datetime"""", """"label"""": """"Last Modified On"""", """"value"""": """"2019-07-29 17:30:00"""", """"description"""": """"obj_client_lastmodifiedon"""", """"placeholder"""": """"[*client_last_modified_on*]"""", """"sequenceorder"""": 40}, {""""type"""": """"numeric"""", """"label"""": """"Last Update"""", """"value"""": 0, """"description"""": """"obj_client_lastupdate"""", """"placeholder"""": """"[*client_last_update*]"""", """"sequenceorder"""": 50}], """"meta"""": {""""class"""": """"obj_client"""", """"dsname"""": """"tbl_obj_client"""", """"version"""": """"1.0.0""""}}"")")

实际上我无法访问记录中的特定字段,此指令使我出错:

RAISE NOTICE '%',v_client.id;
  

v_client.id 22P02的语法不适用于uuid类型。

1 个答案:

答案 0 :(得分:0)

您应该像表而不是标量那样查询函数:

FOR v_client IN SELECT * FROM fn_create_clients('system', 'active', v_com_json, in_debug)
LOOP
    RAISE NOTICE '%', v_client.id;
END LOOP;

因此v_client一次只能包含一行。