Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling functions with exec instead of select

People also ask

Can we use exec in SQL function?

Exec is not allowed in functions, but it is allowed in stored procedures, so you can just rewrite the function as a stored procedure which retuns a resultset.

Can we call function in select statement?

A function can be called in a select statement as well as in a stored procedure. Since a function call would return a value we need to store the return value in a variable. Now creating a stored procedure which calls a function named MultiplyofTwoNumber; see: Create PROCEDURE [dbo].

How do you call a function in Plpgsql?

The normal syntax to call another PL/pgSQL function from within PL/pgSQL is to either reference the function in a SQL SELECT statement, or during the assignment of a variable. For example: SELECT function_identifier ( arguments ); variable_identifier := function_identifier ( arguments );


use PERFORM statement - http://www.postgresql.org/docs/current/static/plpgsql-statements.html

Sometimes it is useful to evaluate an expression or SELECT query but discard the result, for example when calling a function that has side-effects but no useful result value. To do this in PL/pgSQL, use the PERFORM statement

so it's just

DO $$ BEGIN
    PERFORM my_function();
END $$;

PostgreSQL 11:

PostgreSQL 11 supports true stored procedures as pointed out by @AbdisamadKhalif . They support in-procedure transaction control.

Older versions:

Yes, that's the standard way, and yes it's weird.

Usually you'd write such functions as stored procedures and invoke them with the CALL or EXECUTE command. PostgreSQL does not support true stored procedures (multiple result sets, autonomous transactions, and all that) though, only sql-callable user-defined functions.

So the workaround is to SELECT function_name() using the PostgreSQL extension syntax that omits FROM, or SELECT 1 FROM function_name(); to be (somewhat) more standard.

The ODBC driver, JDBC driver, etc understand the {call func_name()} escape syntax and automatically translate it to an underlying SELECT.


You will use from when the function returns a set. If the function returns void just do

select my_function();