SQL 在 postgres 中打印运行时消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22508516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing run time messages in postgres
提问by RAFIQ
Can we use RAISE NOTICE
in postgres as equivalent of RAISERROR
'message to display' WITH NOWAIT
in SQL Server, or is there a better way to print intermediate messages while postgres queries are running? Please suggest if there is better way to print run time messages in postgres.
我们可以RAISE NOTICE
在 postgres 中使用相当于SQL Server 中的RAISERROR
“要显示的消息” WITH NOWAIT
,还是有更好的方法在 postgres 查询运行时打印中间消息?请建议是否有更好的方法在 postgres 中打印运行时消息。
INSERT INTO tbl1 (col1) values (val1);
DO $$
begin
raise notice 'insert tbl1 done!';
end;
$$;
UPDATE tbl2 set col2='val2' where ...;
DO $$
begin
raise notice 'update tbl2 done!';
end;
$$;
I apologize if this code is too bad to comment, pls do suggest a better way to do it, Thanks
如果此代码太糟糕而无法发表评论,我深表歉意,请提出更好的方法,谢谢
回答by Rahul
Yes, you can use RAISE NOTICE
like below. It's correct the way you are doing.
是的,你可以RAISE NOTICE
像下面这样使用。你这样做是正确的。
RAISE NOTICE 'i want to print % and %', var1,var2;
See here for more information https://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html
有关更多信息,请参见此处https://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html
EDIT:
编辑:
begin
INSERT INTO tbl1 (col1) values (val1);
raise notice 'insert tbl1 done!';
end;
回答by Gaurav Koradiya
you can use very simple statement in function everywhere.
您可以在任何地方在函数中使用非常简单的语句。
DO $$ begin raise notice '%',now(); end; $$;
function for reference:
功能参考:
create or replace function test() RETURNS bool AS '
begin
raise notice ''%'',now();
for i IN 0..50000000 loop
end loop
raise notice ''%'',now();
return true;
end;
LANGUAGE 'plpgsql';
语言'plpgsql';
回答by What Would Be Cool
You could also do normal selects without the DO
block.
您也可以在没有DO
块的情况下进行普通选择。
INSERT INTO tbl1 (col1) values (val1);
SELECT 'insert tbl1 done!' as msg;
UPDATE tbl2 set col2='val2' where ...;
SELECT 'update tbl2 done!' as msg;
The tradeoff is that it does add extra clutter to the output, like
权衡是它确实给输出增加了额外的混乱,比如
UPDATE 1
msg
-----------------
update tbl2 done!
(1 row)
回答by Emery Lapinski
RAISE NOTICE is part of PL/pgSQL so it's only legal in a function or an anonymous DO block. I guess you could make a function that raises the notice and call that.
RAISE NOTICE 是 PL/pgSQL 的一部分,所以它只在函数或匿名 DO 块中合法。我想你可以创建一个函数来引发通知并调用它。