我正在创建一个如下所示的存储过程:
CREATE FUNCTION get_components(_given_user_id integer) RETURNS TABLE (id integer, name varchar, active boolean, parent integer, type smallint, description varchar, email varchar, user_id integer, component_id integer, flag smallint) AS $body$
BEGIN
RETURN QUERY SELECT s.* FROM st_components s LEFT JOIN (SELECT a.* FROM st_users_components_perms a WHERE a.user_id=_given_user_id) ON a.component_id=s.id ORDER BY name;
END;
$body$ LANGUAGE plpgsql;
问题是, ON a.component_id=s.id ORDER BY name 不起作用,因为此时 a.component_id 超出范围。有没有办法在查询之外将“a”声明为 st_users_components_perms?这是如何解决的?非常感谢您的任何见解!