X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 487889DC851 for ; Mon, 30 Jan 2006 00:24:33 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 80693-08 for ; Mon, 30 Jan 2006 00:24:31 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id 273599DC843 for ; Mon, 30 Jan 2006 00:24:29 -0400 (AST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id k0U4OP7H035049 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sun, 29 Jan 2006 21:24:27 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.4/8.13.4) with ESMTP id k0U4OOvi056530; Sun, 29 Jan 2006 21:24:24 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id k0U4OOhv056529; Sun, 29 Jan 2006 21:24:24 -0700 (MST) (envelope-from mfuhr) Date: Sun, 29 Jan 2006 21:24:24 -0700 From: Michael Fuhr To: ody quraviharto Cc: pgsql-sql@postgresql.org Subject: Re: variable scooping Message-ID: <20060130042424.GA56410@winnie.fuhr.org> References: <5a3d805c0601291801j7de16f2eq15a4cd9698dbc7e4@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5a3d805c0601291801j7de16f2eq15a4cd9698dbc7e4@mail.gmail.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.114 required=5 tests=[AWL=0.114] X-Spam-Score: 0.114 X-Spam-Level: X-Archive-Number: 200601/214 X-Sequence-Number: 24003 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