使用PostgreSQL调用pgAdmin中的函数时,函数不存在错误消息

时间:2016-10-12 01:48:26

标签: sql postgresql

以下是我在pgAdmin中创建的函数:

CREATE OR REPLACE FUNCTION public.create_patient(
      patient_id integer,
      name character varying,
      sex character varying,
      "DOB" date,
      phone_number character varying,
      address character varying)
  RETURNS integer AS
$BODY$
begin
  INSERT INTO patient
    ("patient_id","name","sex","DOB","phone_number","address")
  VALUES 
    (patient_id,name,sex,DOB,phone_number,address);
  return patient_id;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

但是,当我调用下面的函数时:

SELECT create_patient(nextval('patient_sequence'),'tina','female','2016-12-06','7863334444','541 sw 3 st');

我收到以下错误消息:

ERROR:  function create_patient(bigint, unknown, unknown, unknown, unknown, unknown) does not exist
LINE 1: SELECT create_patient(nextval('patient_sequence'),'tina','fe...
               ^
    HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

如何更改函数以有效插入值?

1 个答案:

答案 0 :(得分:0)

我的猜测是你的序列是一个bigint,但你有一个函数使用patient_id作为整数。将序列更改为整数或将序列的结果转换为整数:

SELECT create_patient(nextval('patient_sequence')::int,'tina','female',
    '2016-12-06','7863334444','541 sw 3 st');

在一个不相关的说明中,由于您引用了DOB列,因此当您使用它时,它会指望引号和文字大写的上下文。因此,我认为您可能需要将功能更改为此(请参阅注释):

CREATE OR REPLACE FUNCTION create_patient(
  patient_id integer,  -- change this to a bigint if you don't want the cast
  name character varying,
  sex character varying,
  "DOB" date,
  phone_number character varying,
  address character varying) RETURNS integer AS
$BODY$
begin
  INSERT INTO patient(
    "patient_id",
    "name",
    "sex",
    "DOB",
    "phone_number",
    "address")
  VALUES (patient_id,
    name,
    sex,
    "DOB",   -- right here
    phone_number,
    address);
  return patient_id;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

我不是百分百肯定,但要注意的是,如果此修复程序无法解决所有问题。