insert in function writen in pgplsql

Hello

lets say I have a function add_user(user varchar(20), password
varchar(20)). In its body I want to have statement INSERT INTO
my_users(user, password) VALUES (user, password); The problem is I cant
- then I try to create such function I get "ERROR: syntax error at or
near "$1"
LINE 1: INSERT INTO my_users( $1 .... " This tells me the parameter is
used instead of column name. That is of course not what I wanted. What
would you recommend to fix this? Is there any way to tell the postgres
that user is column name? I tried "user" with same error. Must I change
the names of parameters?

using PG 8.3

--
Julius Tuskenis
Programavimo skyriaus vadovas
UAB nSoft
mob. +37068233050


--
Sent via pgsql-admin mailing list (pgsql-admin [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Julius Tuskenis [ Fr, 19 März 2010 14:45 ] [ ID #2035714 ]

Re: insert in function writen in pgplsql

Le 19/03/2010 14:45, Julius Tuskenis a =C3=A9crit :
> Hello
>
> lets say I have a function add_user(user varchar(20), password
> varchar(20)). In its body I want to have statement INSERT INTO
> my_users(user, password) VALUES (user, password); The problem is I cant
> - then I try to create such function I get "ERROR: syntax error at or
> near "$1"
> LINE 1: INSERT INTO my_users( $1 .... " This tells me the parameter is
> used instead of column name. That is of course not what I wanted. What
> would you recommend to fix this? Is there any way to tell the postgres
> that user is column name? I tried "user" with same error. Must I change
> the names of parameters?
>

Yes. It would also be easier to debug.


--
Guillaume.
http://www.postgresqlfr.org
http://dalibo.com

--
Sent via pgsql-admin mailing list (pgsql-admin [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Guillaume Lelarge [ Fr, 19 März 2010 15:20 ] [ ID #2035715 ]

Re: insert in function writen in pgplsql

2010.03.19 16:20, Guillaume Lelarge ra=C5=A1=C4=97:
> Yes. It would also be easier to debug.
>
I guess so, but is there no way to tell the postgres that I'm listing
the field names and there is no place for a parameter in that list... I
tried using INSERT INTO table(table.column .... syntax, but postgres
does not accept that.
When you write a new function it is not hard to pick the parameter names
you want, but when editing it you have to play with what you have and
sometimes it's a pain....

One more question Is there a way to use INSERT INTO table($1....
syntax?? I can't think of such case. Can you provide an example?

--
Julius Tuskenis
Programavimo skyriaus vadovas
UAB nSoft
mob. +37068233050


--
Sent via pgsql-admin mailing list (pgsql-admin [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Julius Tuskenis [ Fr, 19 März 2010 15:39 ] [ ID #2035716 ]

Re: insert in function writen in pgplsql

Le 19/03/2010 15:39, Julius Tuskenis a =C3=A9crit :
> 2010.03.19 16:20, Guillaume Lelarge ra=C5=A1=C4=97:
>> Yes. It would also be easier to debug.
>>
> I guess so, but is there no way to tell the postgres that I'm listing
> the field names and there is no place for a parameter in that list... I
> tried using INSERT INTO table(table.column .... syntax, but postgres
> does not accept that.

No, there's no way.

> When you write a new function it is not hard to pick the parameter name=
s
> you want, but when editing it you have to play with what you have and
> sometimes it's a pain....
>

I always add prefix to work around this (p_user for example).

> One more question Is there a way to use INSERT INTO table($1....
> syntax?? I can't think of such case. Can you provide an example?
>

You'll find examples and explanations in the fine manual:


http://www.postgresql.org/docs/8.4/interactive/plpgsql-state ments.html#PL=
PGSQL-STATEMENTS-EXECUTING-DYN


--
Guillaume.
http://www.postgresqlfr.org
http://dalibo.com

--
Sent via pgsql-admin mailing list (pgsql-admin [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Guillaume Lelarge [ Fr, 19 März 2010 15:48 ] [ ID #2035717 ]

Re: insert in function writen in pgplsql

--0016e65c7c20a41b7f04822aa832
Content-Type: text/plain; charset=UTF-8

2010/3/19 Julius Tuskenis <julius [at] nsoft.lt>

> Hello
>
> lets say I have a function add_user(user varchar(20), password
> varchar(20)). In its body I want to have statement INSERT INTO
> my_users(user, password) VALUES (user, password); The problem is I cant -
> then I try to create such function I get "ERROR: syntax error at or near
> "$1"
> LINE 1: INSERT INTO my_users( $1 .... " This tells me the parameter is
> used instead of column name. That is of course not what I wanted. What would
> you recommend to fix this? Is there any way to tell the postgres that user
> is column name? I tried "user" with same error. Must I change the names of
> parameters?
>
> using PG 8.3
>
>
You won't have to change the parameters in the function definition, instead
you can use the RENAME clause:

CREATE OR REPLACE FUNCTION add_user(user varchar(20), password varchar(20) )
RETURNS VOID AS $$
DECLARE
RENAME user TO x_user;
RENAME pa TO x_password;
BEGIN
INSERT INTO my_users(user, password) VALUES (x_user, x_password);
END; LANGUAGE plgpsql;

regards
Szymon

--0016e65c7c20a41b7f04822aa832
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div class=3D"gmail_quote">2010/3/19 Julius Tuskenis <span dir=3D"ltr"><=
<a href=3D"mailto:julius [at] nsoft.lt" target=3D"_blank">julius [at] nsoft.lt</a>>=
;</span><br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;bo=
rder-left:1px #ccc solid;padding-left:1ex">

Hello<br>
<br>
lets say I have a function add_user(user varchar(20), password varchar(20))=
.. In its body I want to have statement INSERT INTO my_users(user, password)=
VALUES (user, password); The problem is I cant - then I try to create such=
function I get "ERROR: =C2=A0syntax error at or near "$1"<b=
r>


LINE 1: INSERT INTO my_users( =C2=A0$1 .... " This tells me the parame=
ter is used instead of column name. That is of course not what I wanted. Wh=
at would you recommend to fix this? Is there any way to tell the postgres t=
hat user is column name? I tried "user" with same error. Must I c=
hange the names of parameters?<br>


<br>
using PG 8.3<br>
<br></blockquote><div><br></div><div>You won't have to change the param=
eters in the function definition, instead you can use the RENAME clause:</d=
iv><div><br></div><div>CREATE OR REPLACE FUNCTION add_user(user varchar(20)=
, password varchar(20) ) RETURNS VOID AS $$</div>

<div>DECLARE</div><div>=C2=A0=C2=A0RENAME user TO x_user;=C2=A0</div><div>=
=C2=A0=C2=A0RENAME pa TO x_password;=C2=A0</div><div>BEGIN</div><div>=C2=A0=
=C2=A0INSERT INTO my_users(user, password) VALUES (x_user, x_password);</di=
v><div>END; LANGUAGE plgpsql;=C2=A0</div>
<div><br></div><div>regards</div><div>Szymon</div></div>

--0016e65c7c20a41b7f04822aa832--
Szymon Guz [ Fr, 19 März 2010 18:27 ] [ ID #2035718 ]

Re: insert in function writen in pgplsql

Szymon Guz <mabewlun [at] gmail.com> writes:
> You won't have to change the parameters in the function definition, instead
> you can use the RENAME clause:

> CREATE OR REPLACE FUNCTION add_user(user varchar(20), password varchar(20) )
> RETURNS VOID AS $$
> DECLARE
> RENAME user TO x_user;
> RENAME pa TO x_password;
> BEGIN

I wouldn't recommend relying on that, because RENAME has been removed in
9.0. I'm not convinced that it would have worked as desired in previous
versions either.

regards, tom lane

--
Sent via pgsql-admin mailing list (pgsql-admin [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Tom Lane [ Fr, 19 März 2010 19:45 ] [ ID #2035719 ]

Re: insert in function writen in pgplsql

--0016e6d9773568202004822c2e4f
Content-Type: text/plain; charset=UTF-8

2010/3/19 Tom Lane <tgl [at] sss.pgh.pa.us>

> Szymon Guz <mabewlun [at] gmail.com> writes:
> > You won't have to change the parameters in the function definition,
> instead
> > you can use the RENAME clause:
>
> > CREATE OR REPLACE FUNCTION add_user(user varchar(20), password
> varchar(20) )
> > RETURNS VOID AS $$
> > DECLARE
> > RENAME user TO x_user;
> > RENAME pa TO x_password;
> > BEGIN
>
> I wouldn't recommend relying on that, because RENAME has been removed in
> 9.0. I'm not convinced that it would have worked as desired in previous
> versions either.
>
> regards, tom lane
>

Is alias also removed?
I've checked that and it works in 8.4

regards,
szymon guz

--0016e6d9773568202004822c2e4f
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<br><br><div class=3D"gmail_quote">2010/3/19 Tom Lane <span dir=3D"ltr"><=
;<a href=3D"mailto:tgl [at] sss.pgh.pa.us">tgl [at] sss.pgh.pa.us</a>></span><br><=
blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px=
#ccc solid;padding-left:1ex;">
<div class=3D"im">Szymon Guz <<a href=3D"mailto:mabewlun [at] gmail.com">mabe=
wlun [at] gmail.com</a>> writes:<br>
> You won't have to change the parameters in the function definition=
, instead<br>
> you can use the RENAME clause:<br>
<br>
> CREATE OR REPLACE FUNCTION add_user(user varchar(20), password varchar=
(20) )<br>
> RETURNS VOID AS $$<br>
> DECLARE<br>
> =C2=A0 RENAME user TO x_user;<br>
> =C2=A0 RENAME pa TO x_password;<br>
> BEGIN<br>
<br>
</div>I wouldn't recommend relying on that, because RENAME has been rem=
oved in<br>
9.0. =C2=A0I'm not convinced that it would have worked as desired in pr=
evious<br>
versions either.<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0regards, tom lane<br>
</blockquote></div><br><div>Is alias also removed?</div><div>I've check=
ed that and it works in 8.4</div><div><br></div><div>regards,</div><div>szy=
mon guz</div>

--0016e6d9773568202004822c2e4f--
Szymon Guz [ Fr, 19 März 2010 20:16 ] [ ID #2035720 ]

Re: insert in function writen in pgplsql

Try

function add_user(_user varchar(20), _password varchar(20))

INSERT INTO my_users(user, password) VALUES (_user, _password);



-----Mensaje original-----
De: pgsql-admin-owner [at] postgresql.org [mailto:pgsql-admin-owner [at] postgresql.o=
rg] En nombre de Julius Tuskenis
Enviado el: viernes, 19 de marzo de 2010 09:45 a.m.
Para: pgsql-admin [at] postgresql.org
Asunto: [ADMIN] insert in function writen in pgplsql

Hello

lets say I have a function add_user(user varchar(20), password
varchar(20)). In its body I want to have statement INSERT INTO
my_users(user, password) VALUES (user, password); The problem is I cant
- then I try to create such function I get "ERROR: syntax error at or
near "$1"
LINE 1: INSERT INTO my_users( $1 .... " This tells me the parameter is
used instead of column name. That is of course not what I wanted. What
would you recommend to fix this? Is there any way to tell the postgres
that user is column name? I tried "user" with same error. Must I change
the names of parameters?

using PG 8.3

--
Julius Tuskenis
Programavimo skyriaus vadovas
UAB nSoft
mob. +37068233050


--
Sent via pgsql-admin mailing list (pgsql-admin [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin


--
Sent via pgsql-admin mailing list (pgsql-admin [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Anibal David Acosta [ Fr, 19 März 2010 16:00 ] [ ID #2035934 ]

Re: insert in function writen in pgplsql

Szymon Guz wrote:
> 2010/3/19 Tom Lane <tgl [at] sss.pgh.pa.us>
>
> > Szymon Guz <mabewlun [at] gmail.com> writes:
> > > You won't have to change the parameters in the function definition,
> > instead
> > > you can use the RENAME clause:
> >
> > > CREATE OR REPLACE FUNCTION add_user(user varchar(20), password
> > varchar(20) )
> > > RETURNS VOID AS $$
> > > DECLARE
> > > RENAME user TO x_user;
> > > RENAME pa TO x_password;
> > > BEGIN
> >
> > I wouldn't recommend relying on that, because RENAME has been removed in
> > 9.0. I'm not convinced that it would have worked as desired in previous
> > versions either.
> >
> > regards, tom lane
> >
>
> Is alias also removed?
> I've checked that and it works in 8.4

ALIAS is the recommended replacement for RENAME:

http://developer.postgresql.org/pgdocs/postgres/release-9-0. html

o Remove PL/pgSQL's RENAME declaration option (Tom)

Instead, use ALIAS, which can now alias any variable, not just dollar
sign variables, e.g. $1.

--
Bruce Momjian <bruce [at] momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

PG East: http://www.enterprisedb.com/community/nav-pg-east-2010.do

--
Sent via pgsql-admin mailing list (pgsql-admin [at] postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Bruce Momjian [ Mo, 22 März 2010 23:47 ] [ ID #2035944 ]
Datenbanken » gmane.comp.db.postgresql.admin » insert in function writen in pgplsql

Vorheriges Thema: Bad encoded chars in being inserted into database
Nächstes Thema: Store database users in custom table