[英]How to RAISE NOTICE in PostgreSQL?
I'm using pgAdmin, and I want to have a simple raise notice;我正在使用 pgAdmin,我想要一个简单的加薪通知; referring to this , I entered
RAISE NOTICE 'note';
参考这个,我输入了
RAISE NOTICE 'note';
and got this error:并得到这个错误:
ERROR: syntax error at or near "RAISE"
LINE 1: RAISE NOTICE 'note';
The only way I could manage to get an output was by using this (which I don't understand well either):我设法获得 output 的唯一方法是使用这个(我也不太明白):
DO $$
BEGIN
RAISE NOTICE 'note';
END;
$$ LANGUAGE plpgsql
And got this output:得到这个 output:
NOTICE: note
DO
Could someone please explain this?有人可以解释一下吗?
RAISE
is a PL/pgSQL command and can only be used inside PL/pgSQL. RAISE
是一个PL/pgSQL命令,只能在 PL/pgSQL 内部使用。 The DO command creates an anonymous PL/pgSQL block (something like a "temporary procedure") and therefor you can use RAISE inside that PL/pgSQL code. DO命令创建一个匿名的 PL/pgSQL 块(类似于“临时过程”),因此您可以在该 PL/pgSQL 代码中使用 RAISE。
RAISE
can not be used in plain SQL , that's why you get the error RAISE
不能用于普通SQL ,这就是你得到错误的原因
Wrap RAISE
into a procedure将
RAISE
包装到一个过程中
create procedure raise_notice (s text) language plpgsql as
$$
begin
raise notice '%', s;
end;
$$;
and call it in SQL并在 SQL 中调用它
call raise_notice('note');
For PG version before 11 create a function that returns void
with the same body and select
from it in SQL对于 11 之前的 PG 版本,创建一个 function,它
returns void
,并在select
中从它返回 select
select raise_notice('note');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.