How to Store the Select Statement Output into Variable Thro Psql ( Postgresql )

Store query result in a variable using in PL/pgSQL

I think you're looking for SELECT select_expressions INTO:

select test_table.name into name from test_table where id = x;

That will pull the name from test_table where id is your function's argument and leave it in the name variable. Don't leave out the table name prefix on test_table.name or you'll get complaints about an ambiguous reference.

Store the whole query result in variable using postgresql Stored procedure

I think you have to read PostgreSQL documentation about cursors.

But if you want just insert data from one table to another, you can do:

insert into data2 (label, number, desc)
select label, number, desc
from data

if you want to "save" data from query, you also can use temporary table, which you can create by usual create table or create table as:

create temporary table temp_data as
(
select label, number, desc
from data
)

see documentation

Store select query as variable and loop for entire variable

Why not run a single query?

insert into b ( . . . )
select . . .
from c
where c.id in (select id from a);

The . . . are for listing the columns/expressions for the insert.

store all the values of a column returned by a select query in a variable - PostgreSQL

If you want to return all values returned, you need to aggregate somehow. As you seem to want a comma separated list, use string_agg(). There is no need to unnest the array though:

CREATE OR REPLACE FUNCTION searchlist(inputlist text) 
RETURNS text AS
$BODY$
DECLARE
newlist text;
BEGIN
select string_agg(transaction_id,',')
into newlist
from incidents
where transaction_id = any (string_to_array(inputlist, ','));

IF coalesce(newlist, '') = '' THEN
return 'Match does not exist';
ELSE
return newList;
END IF;
END;
$BODY$
LANGUAGE plpgsql;

I would highly recommend to not pass comma separated values around. Postgres properly supports arrays and I would use an array as a parameter and and array as the return value as well. You also don't really need PL/pgSQL for this.

A cleaner version (in my opinion) would be:

CREATE OR REPLACE FUNCTION search_list(p_inputlist int[]) 
RETURNS int[]
AS
$BODY$
select array_agg(transaction_id)
from incidents
where transaction_id = any (p_inputlist);
$BODY$
LANGUAGE sql;

The only difference is that this doesn't return a message indicating that the returned array was empty. If you do that, you can use cardinality() in the code that uses the function to check if no rows were returned.

batch file to store the psql query output into a variable

You can use a for loop to parse the output

for /f %%i in ('psql -t -U postgres -d rpd -c "select count(*) from rpd.rpm_rpd_count"') do set A=%%i

(use a single % if you try it in the command line console, and double %% in a batch file)

How do you use script variables in psql?

Postgres variables are created through the \set command, for example ...

\set myvariable value

... and can then be substituted, for example, as ...

SELECT * FROM :myvariable.table1;

... or ...

SELECT * FROM table1 WHERE :myvariable IS NULL;

edit: As of psql 9.1, variables can be expanded in quotes as in:

\set myvariable value 

SELECT * FROM table1 WHERE column1 = :'myvariable';

In older versions of the psql client:

... If you want to use the variable as the value in a conditional string query, such as ...

SELECT * FROM table1 WHERE column1 = ':myvariable';

... then you need to include the quotes in the variable itself as the above will not work. Instead define your variable as such ...

\set myvariable 'value'

However, if, like me, you ran into a situation in which you wanted to make a string from an existing variable, I found the trick to be this ...

\set quoted_myvariable '\'' :myvariable '\''

Now you have both a quoted and unquoted variable of the same string! And you can do something like this ....

INSERT INTO :myvariable.table1 SELECT * FROM table2 WHERE column1 = :quoted_myvariable;


Related Topics



Leave a reply



Submit