up vote 0 down vote favorite

Hi

I have written a pgsql function along the lines of what's shown below. How can I get rid of the $1, $2, etc. and replace them with the real argument names to make the function code more readable?

Regards

Peter

CREATE OR REPLACE FUNCTION InsertUser (
    UserID UUID,
    FirstName CHAR(10),
    Surname VARCHAR(75),
    Email VARCHAR(75)
)
RETURNS void
AS
$$
INSERT INTO "User" (userid,firstname,surname,email)
VALUES ($1,$2,$3,$4)
$$
LANGUAGE SQL;
link|flag

78% accept rate

1 Answer

up vote 1 down vote accepted

Try this thing

    CREATE or replace FUNCTION delhi(nam varchar, mm numeric , nn numeric  ) RETURNS integer
    AS $$
        begin
        insert into exe ( name , m1 ,m2 )  values ( nam, mm , nn );
-- see here column name is not like function argument name , so that it wont say error
           return 1;
    end ;
    $$
    LANGUAGE plpgsql;

Function calling :

select delhi ( 'first value', 2,3 ) ; 
link|flag
ok that seems to work. But not sure why?? All I see that's materially different is that you excluded the explicit column names in the insert statement. Can one not use explicit column names? – Peter Mar 19 at 4:57
and how come it works for you not enclosing the table name in quotes "? When I try Insert into user PGAdmin complains. I need to say Insert into "User" – Peter Mar 19 at 4:59
See, you can give the column name explicit , But that should not be function argument name . If you have function argument name as column name . that is interpret as $1, $2 . – pavun_cool Mar 19 at 5:08

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.