PL/pgSQL procedure return SELECT result

Eine procedure die das Resultat einer SELECT Query zurück gibt:

CREATE OR REPLACE FUNCTION lerp_query_suppliers_for_product_all(p_product_uuid UUID)
    RETURNS TABLE
            (
                SUPPLIER_UUID          UUID,
                SUPPLIER_NO            INTEGER,
                SUPPLIER_NAME          VARCHAR(200),
                SUPPLIER_NAME2         VARCHAR(200),
                SUPPLIER_STREET        VARCHAR(200),
                SUPPLIER_ZIP           VARCHAR(10),
                SUPPLIER_CITY          VARCHAR(200),
                COUNTRY_ID             INTEGER,
                SUPPLIER_TEL           VARCHAR(100),
                SUPPLIER_FAX           VARCHAR(100),
                SUPPLIER_EMAIL         VARCHAR(200),
                SUPPLIER_WWW           VARCHAR(200),
                SUPPLIER_LANG_ISO      ENUM_SUPPORTED_LANG_ISO,
                INDUSTRY_CATEGORY_UUID UUID,
                SUPPLIER_TAX_ID        VARCHAR(20),
                SKR_03_CODE            CHAR(4),
                SUPPLIER_TIME_CREATE   TIMESTAMP,
                SUPPLIER_TIME_UPDATE   TIMESTAMP,
                SUPPLIER_NOTE          TEXT
            )
AS
$$
DECLARE
BEGIN
    RETURN QUERY SELECT s.*
                 FROM supplier s
                 WHERE s.supplier_uuid IN (
                     SELECT DISTINCT po.supplier_uuid
                     FROM purchase_order po
                     WHERE po.purchase_order_uuid IN (
                         SELECT DISTINCT poi.purchase_order_uuid
                         FROM purchase_order_item poi
                         WHERE poi.product_uuid = p_product_uuid
                     )
                 )
                    OR s.supplier_uuid IN (
                     SELECT DISTINCT pr.supplier_uuid
                     FROM purchase_request pr
                     WHERE pr.purchase_request_uuid IN (
                         SELECT DISTINCT pri.purchase_request_uuid
                         FROM purchase_request_item pri
                         WHERE pri.product_uuid = p_product_uuid
                     )
                 )
                    OR s.supplier_uuid IN
                       (SELECT ps.supplier_uuid FROM product_supplier ps WHERE ps.product_uuid = p_product_uuid);
END;

$$ LANGUAGE plpgsql;

ALTER FUNCTION lerp_query_suppliers_for_product_all(UUID) OWNER TO postgres;

…und so ruft man sie auf:

SELECT * FROM lerp_query_suppliers_for_product_all('80ef3210-c6a8-47e4-a50e-0cf5792077d7');

stackoverflow

 

stored procedure/function stub

CREATE OR REPLACE FUNCTION app_get_foo(P_TEST_UUID UUID)
    RETURNS TABLE
            (
                TEST_UUID UUID
            )
    LANGUAGE plpgsql
AS
$$
DECLARE
BEGIN

END;
$$