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

Re: Unexpected results: string vs. direct SQL

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Thangalin <thangalin(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Unexpected results: string vs. direct SQL
Date: 2011-05-31 01:39:49
Message-ID: 14456.1306805989@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-novice
Thangalin <thangalin(at)gmail(dot)com> writes:
> The following code works as expected, returning two columns of data (a row
> number and a valid value):
> ...
> The following code does not work as expected; the first column is a row
> number, the second column is NULL.

I think the problem is that you're assuming "amount" will refer to a
table column of the query, when actually it's a local variable of the
plpgsql function.  The second interpretation will take precedence unless
you qualify the column reference with the table's name/alias.

(BTW, PG 9.0 will throw an error by default when there's an ambiguity of
this type.)

> My second question is tangentially related: how do you use PREPARE inside of
> a stored procedure?

There is no need to use PREPARE, because every SQL query in plpgsql is
effectively prepared automatically.

			regards, tom lane

In response to

pgsql-novice by date

Next:From: Dan HajdukDate: 2011-05-31 01:43:05
Subject: CentOS 5.6
Previous:From: Jean-Yves F. BarbierDate: 2011-05-30 19:29:22
Subject: strange SSL msg

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