Printing a Value of a Variable in Postgresql

printing a value of a variable in postgresql

You can raise a notice in Postgres as follows:

raise notice 'Value: %', deletedContactId;

Read here

Postgres sql to print a variable in loop cycle

I quote here wildplasser comment to the question which actually resolved this issue.

Don't put the return i; inside the loop. See this for returning
sets and sets of rows.

Assigning query output to variable in postgres stored proc

You need an alias for the subquery, for example : as sub

CREATE OR Replace PROCEDURE schema.MyProcedure() 
AS $$

DECLARE
RowCount int = 100;

BEGIN

select cnt into RowCount
from (
Select count(*) as cnt
From schema.MyTable
) as sub ;

RAISE NOTICE 'RowCount: %', RowCount;

END;
$$
LANGUAGE plpgsql;

How can I print text to stdout from a psql query?

\echo prints to stdout in psql. I think RAISE NOTICE goes to stderr.

It is possible to select into a psql variable and then echo it. A basic run-down is at How do you use script variables in PostgreSQL?

However I am not sure of any way to pull a query result into a variable at present.

Assuming that is not the answer you are looking for is just a SELECT.

So something like:

SELECT (EXISTS (....))::int;
-- true is 1, false is 0

Note there is no way to do this from a DO statement so you would have to actually create a function if you need to do this from plpgsql.

store postgresql result in bash variable

Put the -c option just before its argument - the query. Mind also using the additional -t option to get just the tuple value. And of course, use the backticks (`) operator.

Using the -X option is also recommended, as sometimes a .psqlrc file might add some redundant output, as well as the -A option, which disables column aligning (whitespaces).

In order to skip NOTICE or other additional messages, include the -q flag.

vartest=`psql -d $db -U $user -AXqtc "SELECT gid FROM testtable WHERE aid='1'"`


Related Topics



Leave a reply



Submit