如何在pgAdmin中执行pgsql脚本?

16

我想直接从pgAdmin编辑器界面执行一些pgScript

FOR i IN 1..10 LOOP
   PRINT i; -- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loop
END LOOP;

但我总是得到

[ERROR    ] 1.0: syntax error, unexpected character

我也尝试使用do$$...$$来包装代码,但并没有解决问题。

2个回答

30

除了Clodoaldo Neto的回答之外,您也可以尝试这个。

DO
$$
BEGIN
 FOR i IN 1..10 LOOP
       RAISE NOTICE '%', i; -- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loop
 END LOOP;
END
$$

4

没有 PRINT 命令。请使用 raise notice 代替。

create function f()
returns void as $$
begin
    FOR i IN 1..10 LOOP
       raise notice '%', i; -- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loop
    END LOOP;
end;
$$ language plpgsql;

http://www.postgresql.org/docs/current/static/plpgsql.html


错误..我以为使用匿名块,不需要为每个短脚本创建一个函数...不是吗? - David S.
1
@davidshen84 ... 但是你需要一个 DO 块,不能直接编写 PL/PgSQL。 - Craig Ringer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接