1

In REPL it's possible to do something like this:

set @x = 1;
set @d = (select now());
select @x, @d;
+------+---------------------+
| @x   | @d                  |
+------+---------------------+
|    1 | 2018-11-22 16:38:11 |
+------+---------------------+

Now I try to do same in REPL. I've found this:

set var.x = 1;
select current_setting('var.x');

And it works, but I can't find the way how to store query result into variable. I've tried this:

set var.d = (select now());
ERROR:  syntax error at or near "("
LINE 1: set var.d = (select now());
                    ^

select now() into var.d;
ERROR:  schema "var" does not exist

select now() into d;
-- creates new table, which is wrong

d := select now();
ERROR:  syntax error at or near "d"
LINE 1: d := select now();

Could you please help find the solution? I'm ussing psql (PostgreSQL) 10.5.

0

1 Answer 1

1

Im not sure if this is what you are looking for, but there is this a way. You can generate a result and store it to a variable like so:

\set x 1
select now() \gset
select :x, :'now';

Result will be:

 ?column? |           ?column?
----------+-------------------------------
        1 | 2018-11-22 19:50:38.391587+02
(1 row)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.