如何在PostgreSQL中运行ad-hoc脚本?

yeg*_*256 37 postgresql postgresql-9.2

我试图在PostgreSQL 9.2中运行它:

RAISE NOTICE 'hello, world!';
Run Code Online (Sandbox Code Playgroud)

服务器说:

Error : ERROR:  syntax error at or near "RAISE"
LINE 1: RAISE NOTICE 'hello, world!'
        ^
Run Code Online (Sandbox Code Playgroud)

为什么?

Tom*_*eif 75

使用匿名代码块:

DO language plpgsql $$
BEGIN
  RAISE NOTICE 'hello, world!';
END
$$;
Run Code Online (Sandbox Code Playgroud)

使用%以下内容引用变量:

RAISE NOTICE '%', variable_name;
Run Code Online (Sandbox Code Playgroud)

  • 为了缩短它,你可以删除换行符和*language plpgsql* (2认同)

Clo*_*eto 24

raisePL/pgSQL唯一的.

http://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html

create or replace function r(error_message text) returns void as $$
begin
    raise notice '%', error_message;
end;
$$ language plpgsql;

select r('an error message');
NOTICE:  an error message
Run Code Online (Sandbox Code Playgroud)