how to pass the function caller's parameter to inside the function. syntax error at or near "$1"

Started by leaf_yxjabout 14 years ago3 messagesgeneral
Jump to latest
#1leaf_yxj
leaf_yxj@163.com

I want to create a function which use to truncate the table specified by the
caller. I very confused how postgresql pass this parameter into the function
:

as a superuser to execute :
1)
create or replace function d() returns void as $$
analyze;
$$ language sql;

----- this works when i issue select d()

2) this doesn't work

create or replace function v(text) returns void as $$
analyze $1;
$$ language sql;

why???? and how to correct it?

I tried another function for insert

1) this works.

create or replace function insert_f(integer) returns void as $$
insert into t1 values($1);
$$ language sql;

----this works when i issue select insert_f(20);

2) this doesn't work.

create or replace function insert_f(text,integer) returns void as $$
insert into $1 values($2);
$$ language sql;

---- it failed to create the function and give me error : syntax error at or
near "$1".

Please help.

Thanks.

Regards.

Grace

--
View this message in context: http://postgresql.1045698.n5.nabble.com/how-to-pass-the-function-caller-s-parameter-to-inside-the-function-syntax-error-at-or-near-1-tp5601045p5601045.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

#2Merlin Moncure
mmoncure@gmail.com
In reply to: leaf_yxj (#1)
Re: how to pass the function caller's parameter to inside the function. syntax error at or near "$1"

On Wed, Mar 28, 2012 at 11:18 AM, leaf_yxj <leaf_yxj@163.com> wrote:

I want to create a function which use to truncate the table specified by the
caller. I very confused how postgresql pass this parameter into the function
:

as a superuser to execute :
1)
create or replace function d() returns void as $$
analyze;
$$ language sql;

----- this works when i issue select d()

2) this doesn't work

create or replace function v(text) returns void as $$
analyze $1;
$$ language sql;

 why???? and how to correct it?

I tried another function for insert

1) this works.

create or replace function insert_f(integer) returns void as $$
insert into t1 values($1);
$$ language sql;

----this works when i issue select insert_f(20);

2) this doesn't work.

create or replace function insert_f(text,integer) returns void as $$
insert into $1 values($2);
$$ language sql;

---- it failed to create the function and give me error : syntax error at or
near "$1".

Please help.

you're not allowed to parameterize table names. workaround is
plpgsql's EXECUTE.

merlin

#3leaf_yxj
leaf_yxj@163.com
In reply to: Merlin Moncure (#2)
Re: how to pass the function caller's parameter to inside the function. syntax error at or near "$1"

merlin : Thanks.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/how-to-pass-the-function-caller-s-parameter-to-inside-the-function-syntax-error-at-or-near-1-tp5601045p5606816.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.