Postgres using variables in SELECT

Asked

Viewed 1,760 times

0

In Mysql we can do:

SET @pedido = 123;

Afterward:

SELECT @pedido

We have something like that in postgres?

I have looked on the web, but only found in the construction of functions and procedures

The need is because sometimes I need to make some select in several tables in the base having in common a request number, more or less like this:

SET @pedido = 1234;
SELECT * FROM TABELA_1 WHERE (pedido = @pedido);
SELECT * FROM TABELA_2 WHERE (pedido = @pedido);
SELECT * FROM TABELA_3 WHERE (pedido = @pedido);
SELECT * FROM TABELA_4 WHERE (pedido = @pedido);

Each select opens a new tab in the DB manager (this is an application resource)

  • 1

    In psql you can use the variable set value and refer to the contents of the variable with :variable.

  • I tried here set xpedido = 123; select * from table Where request = :request; but it did not work, he complains about the code, I did not understand how to write this variable

  • 2

    If you have set set set set set set 123 then use SELECT * FROM table WHERE request = :xpedido; [https://www.postgresql.org/docs/current/app-psql.html]

  • Sorry I had to repeat both, at the time of typing here I was wrong... but he from syntax error on set I think the version of my postgres does not accept this command I am using 9.6

  • Note that there is no this = in set.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.