Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
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

pgsql-novice by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group