插入函数Postgresql如何用参数测试?

时间:2015-05-24 17:56:56

标签: postgresql function

我在postgresql 9.4中有以下插入函数。 我完全无法用参数进行测试。

DROP FUNCTION "Public".cash_inserts(integer, bigint, timestamp without time zone, character varying, numeric, numeric);

CREATE OR REPLACE FUNCTION "Public".cash_inserts(
cashid integer,
cashserial bigint,
cashdate timestamp without time zone,
cashmemo text,
cashcredit numeric,
cashdebit numeric)
  RETURNS integer AS
$BODY$ 

INSERT INTO "Public"."CashAccounts"
VALUES
    (
        CashId,
        CashSerial,
        CashDate,
        CashMemo,
        CashCredit,
        CashDebit
    ) RETURNING CashId ; 

 $BODY$
  LANGUAGE sql VOLATILE
  COST 100;

ALTER FUNCTION "Public".cash_inserts(integer, bigint, timestamp without time zone, text, numeric, numeric)
  OWNER TO robert100;

因为字段'cashid,cashserial,cashdate'要么自动递增,要么由数据库创建,因此我没有输入任何值。但是,Cashmemo“不是NUll”和“Cashcredit,Cashdebit”可以是可空的或可选的。

2 个答案:

答案 0 :(得分:0)

为什么在函数插入语句中包含cashid,cashserial,cashdate,如果它们是either auto-incrementing or created by the database,如你所说,函数返回int?

也许试试:

DROP FUNCTION "Public".cash_inserts(integer, bigint, timestamp without time zone, character varying, numeric, numeric);
CREATE OR REPLACE FUNCTION "Public".cash_inserts(
cashmemo text,
cashcredit numeric,
cashdebit numeric)
  RETURNS integer AS
$BODY$ INSERT INTO "Public"."CashAccounts"
VALUES
    (
        CashMemo,
        CashCredit,
        CashDebit
    ) RETURNING CashId ; $BODY$
  LANGUAGE sql VOLATILE
  COST 100;
ALTER FUNCTION "Public".cash_inserts(text, numeric, numeric)
  OWNER TO robert100

然后用变量参数调用函数,数据库将自动生成cashid,cashserial和cashdate。

答案 1 :(得分:0)

看来我原来的问题是正确的。我发现,当你提供参数时,它们必须都是这样的引号:

    SELECT "Public".cash_inserts(
    '10',
    '4',
    '12-12-12',
    'COOL',
    '200',
    '300'
);