Re: How to declare a variable in a postgresql query in sql language ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Sara Harris <sarah(at)ips(dot)gov(dot)il>
Cc: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: How to declare a variable in a postgresql query in sql language ?
Date: 2012-01-01 18:01:58
Message-ID: 8437.1325440918@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Sara Harris <sarah(at)ips(dot)gov(dot)il> writes:
> In MS SQL Server I can do this:
> DECLARE @myvar INT
> SET @myvar = 5

> SELECT *
> FROM somewhere
> WHERE something = @myvar

> How do I do the same in PostgreSQL in sql language?

You don't. You need to use plpgsql if you need declared variables.

In practice, if you're coding in a style that would require local
variables, you're almost certainly going to need plpgsql anyway,
since plain SQL hasn't got any control structures either.

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2012-01-01 18:08:06 Re: ERROR: invalid input syntax for integer: SQL state: 22P02
Previous Message Jean-Yves F. Barbier 2012-01-01 17:40:59 Re: is it normal behavior of index?