Re: insert in function writen in pgplsql - Mailing list pgsql-admin

From Szymon Guz
Subject Re: insert in function writen in pgplsql
Date
Msg-id e4edc9361003191027j785b8ba1oc5d5e7e0f59fe076@mail.gmail.com
Whole thread Raw
In response to insert in function writen in pgplsql  (Julius Tuskenis <julius@nsoft.lt>)
Responses Re: insert in function writen in pgplsql  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-admin
2010/3/19 Julius Tuskenis <julius@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

pgsql-admin by date:

Previous
From: Guillaume Lelarge
Date:
Subject: Re: insert in function writen in pgplsql
Next
From: Tom Lane
Date:
Subject: Re: insert in function writen in pgplsql