我在PostgreSQL中有一个如下所示的脚本:
SET SEARCH_PATH TO "foo";
{...500 lines of script...}
SET SEARCH_PATH TO "bar";
{...500 lines of script...}
SET SEARCH PATH TO "baz";
{...500 lines of script...}
...etc
500行脚本是每个模式的字符相同的字符,并且有几个模式。
有没有办法使用每个模式名称的字符串值减少这个循环?
答案 0 :(得分:0)
回答我自己的问题。在dba.stackexchange上阅读this topic后,看起来一般模式是这样的:
DO
$do$
DECLARE _schema text;
BEGIN
FOR _schema IN
SELECT 'foo' UNION
SELECT 'bar' UNION
SELECT 'baz'
LOOP
EXECUTE 'SET LOCAL search_path = "' || _schema || '"';
// Add your SQL scripts here
END LOOP;
END
$do$