Am I entering a world of pain...

Поиск
Список
Период
Сортировка
Искать

Am I entering a world of pain...

От:
Aled Morris <aled@tesco.net>
Дата:
...by trying to port a SQL Server application to Postgresql?

I am unable to get a simple function to work.  Any ideas as to what is wrong 
with it?

create or replace function droptable (varchar(255)) returns void as '
begin
  drop table $1;
  return;
end;
' language 'plpgsql';

This is what happens when I execute the function:

select droptable('asdfadsfasfd');
WARNING:  Error occurred while executing PL/pgSQL function droptable
WARNING:  line 2 at SQL statement
ERROR:  parser: parse error at or near "$1" at character 13

Regards

AM

Re: Am I entering a world of pain...

От:
Stephan Szabo <sszabo@megazone.bigpanda.com>
Дата:

On Tue, 12 Aug 2003, Aled Morris wrote:

> ...by trying to port a SQL Server application to Postgresql?
>
> I am unable to get a simple function to work.  Any ideas as to what is wrong
> with it?
>
> create or replace function droptable (varchar(255)) returns void as '
> begin
>   drop table $1;
>   return;
> end;
> ' language 'plpgsql';
>
> This is what happens when I execute the function:
>
> select droptable('asdfadsfasfd');
> WARNING:  Error occurred while executing PL/pgSQL function droptable
> WARNING:  line 2 at SQL statement
> ERROR:  parser: parse error at or near "$1" at character 13

We don't support using variables directly for object names like that,
you'd have to do something like
 EXECUTE ''drop table '' || $1;

Re: Am I entering a world of pain...

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Aled Morris  writes:
> create or replace function droptable (varchar(255)) returns void as '
> begin
>   drop table $1;
>   return;
> end;
> ' language 'plpgsql';

Try
	execute ''drop table '' || $1;
instead.  See the plpgsql manual's overview for an explanation why you
can't use variables as table/field names except through EXECUTE.

			regards, tom lane
FAQ