Peter
Peter

Reputation: 545

Postgres: Using function variable names in pgsql function

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;

Upvotes: 3

Views: 3459

Answers (2)

falym
falym

Reputation: 51

You must declare it before you use it at the declare area. For exemple :

DECLARE   
    v_UserID alias for $1;
    v_FirstName alias for $2;
    v_Surname alias for $3;
    v_Email alias for $4;
BEGIN
$$
END

Upvotes: 5

Pavunkumar
Pavunkumar

Reputation: 5335

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 ) ; 

Upvotes: 1

Related Questions