Re: plpgsql; execute query inside exists

From: Alban Hertroys <haramrae(at)gmail(dot)com>
To: jozsef(dot)kurucz(at)invitel(dot)hu
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: plpgsql; execute query inside exists
Date: 2011-10-18 08:22:50
Message-ID: CAF-3MvOkChhyo06nf2UtmWU9K1_Zy=+YU7+++9PCGR+GbqkcVA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 18 October 2011 09:57, <jozsef(dot)kurucz(at)invitel(dot)hu> wrote:
> Hi,
>
> Thanks for the reply!
> But I don't want to check if the table exists, I want to see the
> result of the SELECT query, if a row presence or not.

So you want to check that the table contains data? In that case it
makes no sense to create the table if it doesn't contain data. It may
very well exist already.

> The tmp_tbl is a dynamic generated table name, but when I write the
> code without EXECUTE, I get syntax error too.

They were explaining why you got the error, they were not telling you
to leave out EXECUTE for dynamic SQL.

> In this case how can I check if a SELECT has result or not?
>
> SELECT INTO rndmd5 md5(random()::text);
>
> tmp_tbl := 'tbl_tmp_' || rndmd5;
>
>
> IF NOT EXISTS(SELECT * FROM tmp_tbl)
>   THEN
>     END IF;

You really should at least limit the possible amount of results from
that SELECT statement. You're not interested in the results.

Anyway, the way to do this in plpgsql is:

EXECUTE SELECT * FROM tmp_tbl LIMIT 1;
IF NOT FOUND THEN
...
END IF;

--
If you can't see the forest for the trees,
Cut the trees and you'll see there is no forest.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2011-10-18 09:26:09 Log or notice values or rows that cause a constraint violation
Previous Message Craig Ringer 2011-10-18 08:08:08 Re: How to correct: ERROR: permission denied: "RI_ConstraintTrigger_24966" is a system trigger