use*_*686 5 postgresql plpgsql postgresql-9.3
是否可以从 postgres 函数返回多个不同类型的结果集?
就像是:
CREATE OR REPLACE FUNCTION getUserById()
RETURNS setof ???
AS
$$
BEGIN
return query select id, name /* and other columns */ from users where id = 1;
return query select id, phone_number from user_phones where user_id = 1
END
$$
LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
我不想使用连接,因为用户可以使用多个电话。避免使用游标也会很棒。在 MS SQL 中是可能的,我想在 postgres 中做同样的事情。
查找后,找不到更好的解决方案,因为使用了游标。
CREATE FUNCTION load_page(_session INT) RETURNS setof refcursor AS
$$
DECLARE c_top_items refcursor;
DECLARE c_shopping_cart refcursor;
BEGIN
OPEN c_top_items FOR
SELECT t.name, t.description
FROM top_item t
ORDER BY t.popularity DESC
LIMIT 10;
RETURN NEXT c_top_items;
OPEN c_shopping_cart FOR
SELECT c.product_id, c.product_name, c.quantity
FROM shopping_cart c
WHERE c.session_id = _session
ORDER BY c.id;
RETURN NEXT c_shopping_cart;
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
并调用:
BEGIN;
SELECT load_page(mySession);
FETCH ALL IN "<server cursor 1>";
FETCH ALL IN "<server cursor 2>";
COMMIT;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12186 次 |
| 最近记录: |