Thread: variables in psql

variables in psql

From
Keala Jacobs
Date:
Does postgres support the use of variables as in the example below?

update table1 set column1=@variable
where column2='text4column2'
and @variable=(select column3 from table2 where column4='text4column4');

If not, how do I execute a statement like this in postgres?

Attachment

Re: [HACKERS] variables in psql

From
Tom Lane
Date:
Keala Jacobs <keala_jacobs@itd.sterling.com> writes:
> Does postgres support the use of variables as in the example below?

> update table1 set column1=@variable
> where column2='text4column2'
> and @variable=(select column3 from table2 where column4='text4column4');

> If not, how do I execute a statement like this in postgres?

That isn't a particularly compelling example, since it looks like what
you mean is the same as

update table1 set column1 = table2.column3
where table1.column2 = 'text4column2' and table2.column4 = 'text4column4';

I have seen examples where it'd be nice to have an SQL variable that
could hold a value from one statement to the next; currently you have
to do that with a temporary table, which seems rather cumbersome...

BTW, I think this'd be more appropriate in pgsql-sql.
        regards, tom lane