Postgres:在pgsql函数中使用函数变量名

时间:2010-03-19 04:42:00

标签: postgresql function

我已经编写了一个pgsql函数,如下所示。如何摆脱$ 1,$ 2等,并用真实的参数名称替换它们,以使函数代码更具可读性?

此致

彼得

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;

2 个答案:

答案 0 :(得分:2)

您必须在声明区域使用它之前声明它。例如:

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

答案 1 :(得分:1)

尝试这件事

    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;

函数调用:

select delhi ( 'first value', 2,3 ) ;