我开始在 postgres 数据库中学习 sql 中的存储过程和函数。我需要一个例子来帮助我完成我想要完成的事情。
我需要运行一个程序并让它返回结果。例如这样的:
run_query(name):
begin
return select * from employees where first_name = $name
end
end
我想要上面的东西在我运行它时返回结果集。这可能吗?提前谢谢你的帮助!这是我试图创建的功能:
CREATE OR REPLACE FUNCTION test() RETURNS TABLE(id INT, subdomain varchar, launched_on_xxx timestamp, UVs bigint, PVs bigint) AS
'SELECT dblink_connect(''other_DB'');
SELECT c.id as id, c.subdomain, c.launched_on_xxx, COALESCE(SUM(tbd.new_unique_visitors), 0) AS UVs, COALESCE(SUM(tbd.page_views), 0) AS PVs
FROM dblink(''SELECT id, subdomain, launched_on_xxx FROM communities'')
AS c(id int, subdomain character varying, launched_on_xxx timestamp)
LEFT OUTER JOIN days_of_center tbd
ON c.id = tbd.community_id
WHERE c.launched_on_xxx < now()
GROUP BY c.id, c.subdomain, c.launched_on_xxx;
SELECT dblink_disconnect();'
LANGUAGE SQL;