Re: How to declare a variable in a postgresql query in sql language ? - Mailing list pgsql-novice

From Tom Lane
Subject Re: How to declare a variable in a postgresql query in sql language ?
Date
Msg-id 8437.1325440918@sss.pgh.pa.us
Whole thread Raw
In response to How to declare a variable in a postgresql query in sql language ?  (Sara Harris <sarah@ips.gov.il>)
List pgsql-novice
Sara Harris <sarah@ips.gov.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

pgsql-novice by date:

Previous
From: "Jean-Yves F. Barbier"
Date:
Subject: Re: is it normal behavior of index?
Next
From: Tom Lane
Date:
Subject: Re: ERROR: invalid input syntax for integer: SQL state: 22P02