简体   繁体   English

如何在 PostgreSQL 中发出通知?

[英]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 procedureRAISE包装到一个过程中

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.

相关问题 PostgreSQL提升通知表达式以进行本地化 - PostgreSQL Raise Notice Expression for Localization 如何从 Go 应用程序查看 postgresql “提出通知” - how to view postgresql “raise notice” from go app 获取所有关于 PostgreSQL 的 RAISE NOTICE 的通知 - Get all notices of PostgreSQL's RAISE NOTICE 使用||在RAISE NOTICE上抛出错误的PostgreSQL错误 操作者 - PostgreSQL throwing error on RAISE NOTICE with || operator 在PostgreSQL中从RAISE NOTICE写入文件 - Write to a file from RAISE NOTICE in postgresql 如何使用Groovy Sql从PostgreSQL函数获取服务器消息(引发通知) - How to get server messages (raise notice) from PostgreSQL function with Groovy Sql 如何从 PostgreSQL function 获取通过 MyBatis mapper 方法调用的服务器消息(raise notice)? - How to get server messages (raise notice) from PostgreSQL function that was invoked through MyBatis mapper method? 触发器中的 RAISE NOTICE 'text' 是否会减慢 Postgresql 中的查询速度? - Does RAISE NOTICE 'text' in triggers slow down queries in Postgresql? Python2打印postgresql存储过程引发通知 - Python2 print postgresql stored procedure raise notice PostgreSQL:通过ZeosLib / Lazarus从客户端连接捕获RAISE NOTICE - PostgreSQL: Capture RAISE NOTICE from a client connection via ZeosLib/Lazarus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM