【问题标题】:how to execute pgsql script in pgAdmin?如何在 pgAdmin 中执行 pgsql 脚本?
【发布时间】:2015-01-17 15:05:36
【问题描述】:

我想直接从 pgAdmin 编辑器 UI 执行一些 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$$...$$ 包装代码,但没有解决问题。

【问题讨论】:

    标签: postgresql plpgsql pgadmin


    【解决方案1】:

    除了Clodoaldo Neto's Answer。你也可以试试这个

    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
    $$
    

    【讨论】:

      【解决方案2】:

      没有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

      【讨论】:

      • err..我认为使用匿名块,没有必要为每个短脚本创建一个函数...不是吗?
      • @davidshen84 ... 但是你需要一个DO 块,你不能直接写PL/PgSQL。
      猜你喜欢
      • 2012-11-18
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 2020-01-20
      • 2022-12-12
      • 1970-01-01
      • 2020-03-23
      • 2022-01-27
      相关资源
      最近更新 更多