postgresql 如何创建不返回任何内容的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14216716/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:57:51  来源:igfitidea点击:

How to create function that returns nothing

postgresqlfunctionplpgsqlvoid

提问by Kabi

I want to write a function with pl/pgsql. I'm using PostgresEnterprise Manager v3and using shell to make a function, but in the shell I must define return type. If I don't define the return type, I'm not able to create a function.

我想用pl/pgsql. 我正在使用PostgresEnterprise Manager v3并使用 shell 创建一个函数,但在 shell 中我必须定义返回类型。如果我不定义返回类型,我将无法创建函数。

How can create a function without return result, i.e a Function that creates a new table?

如何创建没有返回结果的函数,即创建新表的函数?

回答by sqreept

Use RETURNS voidlike below:

使用RETURNS void如下:

CREATE FUNCTION stamp_user(id int, comment text) RETURNS void AS $$
    #variable_conflict use_variable
    DECLARE
        curtime timestamp := now();
    BEGIN
        UPDATE users SET last_modified = curtime, comment = comment
          WHERE users.id = id;
    END;
$$ LANGUAGE plpgsql;

回答by David Climent

Functions must always return something, although you can use procedures like

函数必须总是返回一些东西,虽然你可以使用像

do $$

and start with normal function like

并从正常功能开始,例如

declare
...

but if you still want to do a function just add voidafter returns.

但如果你仍然想做一个函数,只需在返回后添加void