我创建了自定义数据类型。因为我给了一个字段的别名。你将在下面的函数体中得到它。
create type voucher as (
ori numeric, RECEIPT_NO numeric
, receipt_date timestamp with time zone, reg_no character varying
, patient_name character varying, tot_refund_bill_amount double precision
, username character varying );
上述声明成功完成 然后我想创建一个函数:
create or replace function billing.voucher_receipt (in_from_date timestamp with time zone, in_to_date timestamp with time zone)
returns setof voucher as $$
declare
out_put voucher%rowtype;
begin
return query(select C.receipt_no as ori ,A.RECEIPT_NO, receipt_date , A.reg_no, patient_name, tot_refund_bill_amount, username
from billing.tran_counter_receipt as a inner join mas_user as b on a.ent_by=b.uid AND cash_book='REFUND'
INNER JOIN billing.tran_BILL AS C ON C.REG_NO=A.REG_NO AND C.CASH_BOOK='GCASH' where receipt_date>=in_from_date and receipt_date<=in_to_date);
end;$$
LANGUAGE plpgsql
执行没有问题。
但是当我用这样的输入调用它时:
select * from voucher_receipt ('2014-09-25 11:42:44.298346+05:30'
, '2014-09-29 11:03:47.573049+05:30')
显示错误:
ERROR: function voucher_receipt(unknown, unknown) does not exist LINE 1: select * from voucher_receipt ('2014-09-25 11:42:44.298346+0... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
任何人都可以帮助我吗?
答案 0 :(得分:1)
您在架构billing
中创建了函数,其中包含:
create or replace function billing.voucher_receipt( ...
然后你打电话没有架构资格:
select * from voucher_receipt ( ...
这仅适用于search_path
的当前设置包含架构billing
。
您无需创建复合类型。除非您需要在多个位置使用相同的类型,否则只需使用RETURNS TABLE
在函数中定义返回类型:
CREATE OR REPLACE FUNCTION billing.voucher_receipt (_from timestamptz
, _to timestamptz)
RETURNS TABLE (
ori numeric
, receipt_no numeric
, receipt_date timestamptz
, reg_no varchar
, patient_name varchar
, tot_refund_bill_amount float8
, username varchar) AS
$func$
BEGIN
RETURN QUERY
SELECT b.receipt_no -- AS ori
, cr.RECEIPT_NO
, ??.receipt_date
, cr.reg_no
, ??.patient_name
, ??.tot_refund_bill_amount
, ??.username
FROM billing.tran_counter_receipt cr
JOIN billing.tran_bill b USING (reg_no)
JOIN mas_user u ON u.uid = cr.ent_by
WHERE ??.receipt_date >= _from
AND ??.receipt_date <= _to
AND b.CASH_BOOK = 'GCASH'
AND ??.cash_book = 'REFUND'
END
$func$ LANGUAGE plpgsql;
timestamptz
时,请勿将参数称为“日期”。RETURN QUERY
不需要括号。DECLARE out_put voucher%rowtype;
RETURNS TABLE
中的列名几乎无处不在函数体中可见。 table-qualify查询中的列,以避免歧义(和错误)。替换我在代码中留下的所有??.
,其中缺少信息。RETURNS
声明中的名称取代。因此AS ori
列表中的SELECT
只是文档,在这种情况下。billing.tran_bill
但不符合mas_user
?