您可能需要添加显式类型转换

Mar*_*ars 4 postgresql

我正在尝试创建返回最后插入的 id 的函数,但在函数调用时显示错误:您能帮我识别我的错误吗?

错误:函数 public.insert_voucher(整数,未知,未知,整数,整数,未知) 不存在
第 1 行:选择 public.insert_voucher(1, 'P', '20180909', 1, 1, 'txt');

功能:

CREATE OR REPLACE FUNCTION public.insert_voucher(
     in_orgid smallint
    ,in_transtype character
    ,in_date character
    ,in_partnerid smallint
    ,in_quantity smallint
    ,in_remarks character varying)
  RETURNS integer AS
$BODY$
BEGIN
insert into 
public.transaction_header(
   org_id
  ,trans_type
  ,fiscal_year
  ,date
  ,partner_id
  ,quantity
  ,remarks
  ,create_by
  ,create_ts)
values (
   in_orgid
  ,in_transtype
  ,1819
  ,in_date
  ,in_partnerid
  ,in_quantity
  ,in_remarks
  ,1
  ,now())
returning trans_header_id;
END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 1;
Run Code Online (Sandbox Code Playgroud)

表架构:

trans_header_id integer NOT NULL DEFAULT nextval('transaction_header_trans_header_id_seq'::regclass)
,org_id smallint NOT NULL
,trans_type character(1) NOT NULL DEFAULT 'P'::bpchar
,fiscal_year smallint NOT NULL DEFAULT '1819'::smallint
,date date NOT NULL
,partner_id smallint NOT NULL
,quantity smallint NOT NULL
,remarks character varying(100)
,create_by smallint NOT NULL
,create_ts timestamp without time zone NOT NULL DEFAULT now()
,update_by smallint
,update_ts timestamp without time zone
,CONSTRAINT transaction_header_pk PRIMARY KEY (trans_header_id)
,CONSTRAINT create_by FOREIGN KEY (create_by)
REFERENCES public.app_user (user_id) MATCH FULL
          ON UPDATE NO ACTION ON DELETE NO ACTION
,CONSTRAINT org_id FOREIGN KEY (org_id)
          REFERENCES public.organization (org_id) MATCH FULL
          ON UPDATE NO ACTION ON DELETE NO ACTION
,CONSTRAINT partner_id FOREIGN KEY (partner_id)
          REFERENCES public.partners (partner_id) MATCH FULL
          ON UPDATE NO ACTION ON DELETE NO ACTION
,CONSTRAINT update_by FOREIGN KEY (update_by)
          REFERENCES public.app_user (user_id) MATCH FULL
          ON UPDATE NO ACTION ON DELETE NO ACTION
,CONSTRAINT org_fy_transtype_transno UNIQUE (org_id, trans_type, fiscal_year)
Run Code Online (Sandbox Code Playgroud)

参考@muistooshort和@stickybit,我正在更新以前的函数。希望它能提供更多的清晰度并返回预期的结果。

CREATE OR REPLACE FUNCTION public.insert_voucher(
    IN in_orgid smallint
    ,IN in_transtype character
    ,IN in_date date
    ,IN in_partnerid smallint
    ,IN in_quantity smallint
    ,IN in_remarks character varying
    ,OUT out_id smallint)
  RETURNS smallint AS
$BODY$
    BEGIN
    insert into 
    public.transaction_header(
       org_id
      ,trans_type
      ,fiscal_year
      ,date
      ,partner_id
      ,quantity
      ,remarks
      ,create_by
      ,create_ts)
    values (
       in_orgid
      ,in_transtype
      ,1819
      ,in_date
      ,in_partnerid
      ,in_quantity
      ,in_remarks
      ,1
      ,now())
    RETURNING trans_header_id
    INTO out_id;
    END
    $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 1;
Run Code Online (Sandbox Code Playgroud)

sti*_*bit 6

ssmallint就是问题所在。integer从到 的转换smallint可能意味着丢失一些信息。引擎不会执行隐式转换,否则信息可能会丢失。因此它认为public.insert_voucher(integer, unknown, unknown, integer, integer, unknown)不是一个选择。

如果您明确地将数字转换为smallint,则调用应该有效。

SELECT public.insert_voucher(1::smallint, 'P', '20180909', 1::smallint, 1::smallint, 'txt');
Run Code Online (Sandbox Code Playgroud)

还有一些其他问题,例如为什么将日期作为字符串传递以及'1819'::smallint(为什么首先传递字符串?)。一旦通话成功,可能会出现其他一些问题。但这超出了当前的问题。