Thread: plpgsql function question

plpgsql function question

From
Scott Geller
Date:

Hi

I am learning plpgsql and am getting errors on this simple function:

CREATE OR REPLACE FUNCTION test2(tab1 text, tab2 text )
  RETURNS text AS
  $$
  BEGIN
  EXECUTE 'create table ' || quote_ident(tab2) || ' as Select *  from ' || quote_ident(tab1);
    RETURN 'done';
  END; $$
  LANGUAGE 'plpgsql' VOLATILE;

Yet, when I replace tab1 & tab2 in the execute statement it works just fine:

CREATE OR REPLACE FUNCTION test2(tab1 text, tab2 text )
  RETURNS text AS
  $$
  BEGIN
  EXECUTE 'create table ' || quote_ident($2) || ' as Select *  from ' || quote_ident($1);
    RETURN 'done';
  END; $$
  LANGUAGE 'plpgsql' VOLATILE;

Any insight would be appreciated!

Thanks

Scott

Re: plpgsql function question

From
Andreas Kretschmer
Date:
Scott Geller <sgellergsu@gmail.com> wrote:

>
> Hi
>
> I am learning plpgsql and am getting errors on this simple function:
>
> CREATE OR REPLACE FUNCTION test2(tab1 text, tab2 text )
>   RETURNS text AS
>   $$
>   BEGIN
>   EXECUTE 'create table ' || quote_ident(tab2) || ' as Select *  from ' ||
> quote_ident(tab1);
>     RETURN 'done';
>   END; $$
>   LANGUAGE 'plpgsql' VOLATILE;

Works for me, without errors:

test=# create table tab1(i int);
CREATE TABLE
Zeit: 0,850 ms
test=*# CREATE OR REPLACE FUNCTION test2(tab1 text, tab2 text )
  RETURNS text AS
  $$
  BEGIN
  EXECUTE 'create table ' || quote_ident(tab2) || ' as Select *  from ' || quote_ident(tab1);
    RETURN 'done';
  END; $$
  LANGUAGE 'plpgsql' VOLATILE;
CREATE FUNCTION
Zeit: 0,472 ms
test=*# select * from test2('tab1','tab2');
 test2
-------
 done
(1 Zeile)

Zeit: 1,844 ms


Please show the error you got.



Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°

Re: plpgsql function question

From
Scott Geller
Date:
Andreas

Thanks for the response... it seems to be working now - I hate it when that happens



On Sun, Apr 4, 2010 at 1:29 AM, Andreas Kretschmer <akretschmer@spamfence.net> wrote:
Scott Geller <sgellergsu@gmail.com> wrote:

>
> Hi
>
> I am learning plpgsql and am getting errors on this simple function:
>
> CREATE OR REPLACE FUNCTION test2(tab1 text, tab2 text )
>   RETURNS text AS
>   $$
>   BEGIN
>   EXECUTE 'create table ' || quote_ident(tab2) || ' as Select *  from ' ||
> quote_ident(tab1);
>     RETURN 'done';
>   END; $$
>   LANGUAGE 'plpgsql' VOLATILE;

Works for me, without errors:

test=# create table tab1(i int);
CREATE TABLE
Zeit: 0,850 ms
test=*# CREATE OR REPLACE FUNCTION test2(tab1 text, tab2 text )
 RETURNS text AS
 $$
 BEGIN
 EXECUTE 'create table ' || quote_ident(tab2) || ' as Select *  from ' || quote_ident(tab1);
   RETURN 'done';
 END; $$
 LANGUAGE 'plpgsql' VOLATILE;
CREATE FUNCTION
Zeit: 0,472 ms
test=*# select * from test2('tab1','tab2');
 test2
-------
 done
(1 Zeile)

Zeit: 1,844 ms


Please show the error you got.



Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°

--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice