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
With Hibernate, How to Query a Table and Return a Hashmap with Key Value Pair Id>Name
Is Order by Clause Allowed in a Subquery
Order of Ands in Where Clause for Greatest Performance
Bigquery Group_Concat and Order By
What SQL Databases Support Subqueries in Check Constraints
Select Second Most Minimum Value in Oracle
How to Generate a Hierarchy Path in SQL That Leads to a Given Node
How to Specify 'Default' as a SQL Parameter Value in Ado.Net
How to Get Windows Log-In User Name for a SQL Log in User
Workaround for Ora-00997: Illegal Use of Long Datatype
How to Read a Text File Using T-Sql
There Is Already an Object Named '#Columntable' in the Database
Get Size of Large Object in Postgresql Query
SQL Query to Search for Room Availability
Entity Framework Hitting 2100 Parameter Limit