Re: variable scooping

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: ody quraviharto <odyyudah(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: variable scooping
Date: 2006-01-30 04:24:24
Message-ID: 20060130042424.GA56410@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Mon, Jan 30, 2006 at 09:01:37AM +0700, ody quraviharto wrote:
> "declare tbl_name varchar:='tbl_A';
> begin
> if exists(select 1 from pg_tables where tablename=tbl_name) then
> select count(*) from tbl_name;
> end if;
> end"
>
> the message was: syntax error in $1 in "select count(*) from $1".

You'll need to use EXECUTE for this query; see "Executing Dynamic
Commands" in the PL/pgSQL documentation. Here's the link for 8.1
(but use the documentation for whatever version you're running):

http://www.postgresql.org/docs/8.1/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

--
Michael Fuhr

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message AKHILESH GUPTA 2006-01-30 06:38:54 regarding debugging?
Previous Message Daniel CAUNE 2006-01-30 03:40:46 Re: Function with default value?