Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value from anonymous function postgresql

How to?

For easy example. I have a simple function:

DO LANGUAGE plpgsql $$ DECLARE BEGIN EXECUTE 'SELECT NOW()'; END $$; 

How I can return value of "NOW()" or other values from also anonymous function? The function is given as an example I have a more complex function.

like image 481
arturgspb Avatar asked Apr 25 '12 11:04

arturgspb


People also ask

What is anonymous block in PostgreSQL?

PostgreSQL started supporting anonymous blocks with version 9.0. Here “code” can be considered as the body of a function with no parameters, which is going to return void and be parsed and executed one time only (i.e., not going to be stored in database catalog).

How do I get an array of films in PostgreSQL?

PostgreSQL returns a table with one column that holds the array of films. In practice, you often process each individual row before appending it in the function’s result set. The following example illustrates the idea. We have created a function with the similar name get_film (varchar, int) but accepts two parameters:

What is the difference between PostgreSQL and PL?

PL/pgSQL (Procedural Language/PostgreSQL) is a procedural language where you can perform more complex tasks than in SQL—like easy computation—and also make use of loops, functions, and triggers. PL/pgSQL code is managed in blocks (block structured code), into which anonymous blocks, functions, and procedures are organized.

What does insert() return do in a PL/pgSQL block?

insert ... returning ... produces a result set that contains columns referenced in the returning clause. In a PL/pgSQL block that result set must be dealt with somehow. You have three options:


1 Answers

DO LANGUAGE plpgsql $$ DECLARE BEGIN execute ' create temporary table t as SELECT NOW() '; END $$;  select * from t; 
like image 146
Clodoaldo Neto Avatar answered Oct 06 '22 06:10

Clodoaldo Neto