pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Erik Wienhold <ewie@ewie.name>
To: J.A. <postgresql@world-domination.com.au>
To: pgsql-general@lists.postgresql.org
Subject: Re: PL/pgSQL doesn't support variables in queries?
Date: Wed, 3 May 2023 14:39:45 +0200 (CEST)
Message-ID: <658455455.1152531.1683117585087@office.mailbox.org> (raw)
In-Reply-To: <CALT+_gvfHtF5jqQg+J2suw3hOEh3ZWjZQDB8jJ945o4b1ekyyQ@mail.gmail.com>
References: <CALT+_gvfHtF5jqQg+J2suw3hOEh3ZWjZQDB8jJ945o4b1ekyyQ@mail.gmail.com>

> On 03/05/2023 14:25 CEST J.A. <postgresql@world-domination.com.au> wrote:
>
> ms-sql person here migrating over to pgsql. One of the first thing's I noticed
> with pgsql (or more specifically, PL/pgSQL) is that it doesn't support
> "variables" in a query?
>
> for example, here's some T-SQL:
>
> DECLARE @fkId INTEGER
>
> SELECT @fkId = fkId FROM SomeTable WHERE id = 1
>
> -- and then do something with that value..
>
> SELECT * FROM AnotherTable WHERE Id = @fkId
> SELECT * FROM YetAnotherTable WHERE FKId = @fkId
> -- etc..

plpgsql does support variable declarations [0] but does not use any special
notation like T-SQL.  An equivalent to your example would be:

	DO $$
	DECLARE
	  v_fkid int;
	  v_rec record;
	BEGIN
	  SELECT fkid INTO v_fkid FROM SomeTable WHERE id = 1;
	  SELECT * INTO v_rec FROM AnotherTable WHERE Id = v_fkid;
	  -- Do something with v_rec ...
	END $$;

Prefixing variable names with v_ is just a convention to avoid ambiguous column
references (assuming that column names are not prefixed with v_) [1].

[0] https://www.postgresql.org/docs/current/plpgsql-declarations.html
[1] https://www.postgresql.org/docs/current/plpgsql-implementation.html#PLPGSQL-VAR-SUBST

--
Erik





view thread (9+ messages)  latest in thread

Message-ID: <658455455.1152531.1683117585087@office.mailbox.org>
Permalink:  ../658455455.1152531.1683117585087@office.mailbox.org/
Also on:    postgresql.org/message-id/658455455.1152531.1683117585087@office.mailbox.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ewie@ewie.name, postgresql@world-domination.com.au
  Subject: Re: PL/pgSQL doesn't support variables in queries?
  In-Reply-To: <658455455.1152531.1683117585087@office.mailbox.org>
  References: <CALT+_gvfHtF5jqQg+J2suw3hOEh3ZWjZQDB8jJ945o4b1ekyyQ@mail.gmail.com> <658455455.1152531.1683117585087@office.mailbox.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox