views:

234

answers:

1

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;
+1  A: 

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 ) ; 
pavun_cool
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
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
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
Peter, the keyword "User" is reserved in Postgres, as a result you must double quote it to use it as a table.
chotchki