How to create function that returns nothing
Asked Answered
B

5

165

I want to write a function with pl/pgsql. I'm using PostgresEnterprise Manager v3 and 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.

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

Bread answered 8/1, 2013 at 14:9 Comment(1)
N
264

Use RETURNS void like below:

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;
Neper answered 8/1, 2013 at 14:13 Comment(5)
For other readers, note that the #variable_conflict directive has nothing to do with the rest of the answer. It's just part of an example function; the only important bit is the RETURNS void. Also, cool, I didn't know PL/PgSQL had pragmas.Possession
Here's a related case making use of #variable_conflict: dba.stackexchange.com/a/105828/3684Twofold
How do I make use of this function inside of another function? If I try without SELECT * FROM stamp_user(...), then I get error: query has no destination for result data and if I just write stamp_user(...) then I get syntax error.Villainy
@Villainy What you are saying doesn't make total sense. However, If you don't want to do anything with the SELECT data use PERFROM. If you do. you will want to SELECT * INTO _record Where _record is defined with DECLARE _record RECORD; before the BEGIN statementChigger
Late to the question, but replying to @pir's comment and to future readers for clarity's sake, there was a misunderstanding in the comment's question and response. When invoking your function, If you see the error query has no destination for result data, you are most probably using SELECT IN YOUR FUNCTION. So replace the SELECT with PERFORM in your function, and then call your function as per normal using SELECT yourFunction(). PERFORM is only valid in pl/pgsql context. See linkActuality
G
13

PostgreSQL 11+: PROCEDUREs

PostgreSQL 11 introduces PROCEDUREs which are basically functions that return nothing, but called with CALL rather than SELECT,

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

Like this,

=# CREATE PROCEDURE create_table_foo()
AS $$
  CREATE TABLE foo ( id int )
$$ LANGUAGE sql;

=# CALL create_table_foo();


=# \d foo;
                Table "public.foo"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 id     | integer |           |          | 
Gretchengrete answered 23/11, 2021 at 6:1 Comment(0)
W
1

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 void after returns.

Weingarten answered 8/4, 2020 at 9:51 Comment(0)
B
1

You can create the PL/pgSQL function which returns nothing with VOID type and with or without RETURN; as shown below. *Be careful, if you return a value with e.g. RETURN 2; for VOID type, then you get the error:

CREATE FUNCTION my_func() RETURNS VOID AS $$
BEGIN                           -- ↑ Here
RETURN; -- Here
END;
$$ LANGUAGE plpgsql;

Or:

CREATE FUNCTION my_func() RETURNS VOID AS $$
BEGIN                           -- ↑ Here
-- RETURN; -- Here
END;
$$ LANGUAGE plpgsql;

In addition, you can create the SQL function which returns nothing with VOID type as shown below. *A SQL function cannot have RETURN statement:

CREATE FUNCTION my_func() RETURNS VOID AS $$
$$ LANGUAGE SQL;
Billibilliard answered 6/12, 2023 at 22:41 Comment(0)
A
0

Adding onto the accepted answer, if you see the error query has no destination for result data when invoking your function, you are most probably using SELECT in YOUR FUNCTION in one of your statements. Replace the SELECT with PERFORM, and then call your function as per normal using SELECT yourFunction().

Note: PERFORM is only valid in pl/pgsql context. See link

Actuality answered 14/11, 2022 at 4:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.