pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
variable scooping
2+ messages / 2 participants
[nested] [flat]

* variable scooping
@ 2006-01-30 02:01  ody quraviharto <odyyudah@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: ody quraviharto @ 2006-01-30 02:01 UTC (permalink / raw)
  To: pgsql-sql

hi all,
I'm a newbie in postgresql. I've tried to make function but having
trouble in variable scooping.
here is the code in plpgsql:

"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". I
thought variable 'tbl_name' was not recognized. Why ?
Please need some help here.
Thx very much.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: variable scooping
@ 2006-01-30 04:24  Michael Fuhr <mike@fuhr.org>
  parent: ody quraviharto <odyyudah@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Michael Fuhr @ 2006-01-30 04:24 UTC (permalink / raw)
  To: ody quraviharto <odyyudah@gmail.com>; +Cc: 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-...

-- 
Michael Fuhr



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2006-01-30 04:24 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-01-30 02:01 variable scooping ody quraviharto <odyyudah@gmail.com>
2006-01-30 04:24 ` Michael Fuhr <mike@fuhr.org>

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