我可以在postgres函数定义中使用有意义的参数名而不是$ 1,$ 2吗?

时间:2015-06-05 19:32:53

标签: postgresql function parameters

我可以使用我在参数列表中放置的有意义的名称,而不是函数定义中的$ 1,$ 2吗?

CREATE OR REPLACE FUNCTION add_report_email(
    reportname1 text,
    personid2 integer )
  RETURNS integer AS
$$
DECLARE reportid integer;
BEGIN

    select report_id into reportid from reports where report_name = $1;    

    if (reportid is null) then
    insert into reports (report_name) values($1) returning report_id into reportid;
    end if;

    insert into emails_sent (pid, report_id, date_sent)
    values($2, reportid, now());

END $$
LANGUAGE plpgsql;

1 个答案:

答案 0 :(得分:0)

是的,显然我可以在函数定义中直接使用参数名称而不会出现问题。例如

CREATE OR REPLACE FUNCTION add_report_email(
    reportname text,
    personid integer )
  RETURNS void AS
$$
DECLARE reportid integer;
BEGIN

    -- add the report name into reports table if it does not exist
    select report_id into reportid from reports where report_name = lower(reportname);

    if (reportid is null) then
  insert into reports (report_name) values(lower(reportname)) returning report_id into reportid;
    end if;

    insert into report_emails_sent (pid, report_id, date_sent)
    values(personid, reportid, now());  
END $$
LANGUAGE plpgsql;