I'm using PostgreSQL
And I want to print a message to the console.
我正在使用PostgreSQL,我想向控制台打印一条消息。
If I use plpythonu
I use plpy.notice
If I use plpgsql
I use raise notice
如果我用plpythonu,我用的是plpy。请注意,如果我使用plpgsql,我会使用raise notice
but how do I print when the function is pure SQL
? using SELECT 'string'
isn't helping me as it print the string in a column and if the message is located in the middle of the code it doesn't show it. I want something like raise notice
/ plpy.notice
for SQL
.
但是,当函数是纯SQL时,如何打印?使用SELECT 'string'对我没有帮助,因为它在列中打印字符串,如果消息位于代码的中间,它就不会显示它。我想要加薪通知。注意SQL。
CREATE OR REPLACE FUNCTION A()
RETURNS VOID AS
$BODY$
how do i print 'hello world' here?
$BODY$
LANGUAGE sql VOLATILE
if it was a plpgsql
I would do:
如果是plpgsql,我会这样做:
CREATE OR REPLACE FUNCTION A()
RETURNS VOID AS
$BODY$
Raise Notice 'hello world'
$BODY$
LANGUAGE plpgsql VOLATILE
I'm looking for the equivalent in LANGUAGE SQL
我在SQL语言中寻找等价的
2 个解决方案
#1
2
In plain SQL there is the NOTIFY
command. That command sends a payload to a channel. The channel and the payload can be any string.
在普通SQL中有NOTIFY命令。该命令向通道发送有效负载。通道和负载可以是任何字符串。
CREATE OR REPLACE FUNCTION A() RETURNS void AS $BODY$
NOTIFY msg, 'hello world';
$BODY$ LANGUAGE sql;
and then:
然后:
LISTEN msg;
SELECT A();
Asynchronous notification "msg" with payload "hello world" received from server process with PID 3275.
UNLISTEN msg;
The message will also have some information on where it originated from. If you really only want the string (the payload), then you would have to parse that out of the full string.
该消息还将提供一些信息,说明它的起源。如果您真的只想要字符串(有效负载),那么您必须从整个字符串中解析它。
#2
3
you can only select it in SQL
只能在SQL中选择它
db=# CREATE FUNCTION b() RETURNS text AS
$BODY$
select 'Hello there'::text;
$BODY$
LANGUAGE sql VOLATILE;
CREATE FUNCTION
db=# select b()::text;
b
-------------
Hello there
(1 row)
#1
2
In plain SQL there is the NOTIFY
command. That command sends a payload to a channel. The channel and the payload can be any string.
在普通SQL中有NOTIFY命令。该命令向通道发送有效负载。通道和负载可以是任何字符串。
CREATE OR REPLACE FUNCTION A() RETURNS void AS $BODY$
NOTIFY msg, 'hello world';
$BODY$ LANGUAGE sql;
and then:
然后:
LISTEN msg;
SELECT A();
Asynchronous notification "msg" with payload "hello world" received from server process with PID 3275.
UNLISTEN msg;
The message will also have some information on where it originated from. If you really only want the string (the payload), then you would have to parse that out of the full string.
该消息还将提供一些信息,说明它的起源。如果您真的只想要字符串(有效负载),那么您必须从整个字符串中解析它。
#2
3
you can only select it in SQL
只能在SQL中选择它
db=# CREATE FUNCTION b() RETURNS text AS
$BODY$
select 'Hello there'::text;
$BODY$
LANGUAGE sql VOLATILE;
CREATE FUNCTION
db=# select b()::text;
b
-------------
Hello there
(1 row)