postgresql存储过程与几个模式的查询

时间:2010-08-23 10:01:08

标签: postgresql stored-procedures schema

在对前一个问题(request over several schema)做出一些回答之后,我尝试编写一个存储过程来为多个模式选择表(每个用户都有一个模式)。

create or replace public.select_simulations() returns setof simulation as $$
declare 
    users pg_user%ROWTYPE;
    simu simulation%ROWTYPE;
begin
    for users in select usename from pg_user where usename <> 'postgres' loop
        for simu in select id, name from (users.usename).simulation loop            
            return next simu;
        end loop;
    end loop;
end; 
$$

但它不接受(users.usename).simulation,如果没有括号则产生错误(似乎搜索子字段,而不是模式)......

那么告诉users.usename是模式名称的正确语法是什么?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以查看for-in-execute控制结构:

FOR record_or_row IN EXECUTE text_expression LOOP 
    statements
END LOOP [ label ];

http://www.postgresql.org/docs/8.1/static/plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERATING

类似的东西:

...

for users in select usename from pg_user where usename <> 'postgres' loop
    for simu in execute 'select id, name from '||quote_ident(users.usename)||'.simulation' loop
        return next simu;
    end loop;
end loop;
...