从postgres函数需要不同的返回格式

时间:2014-07-18 22:39:04

标签: postgresql

我有一张桌子

CREATE TABLE reward_transactions
(
  uid bigint NOT NULL,
  reward_type text NOT NULL,
  count bigint,
  last_update timestamp with time zone DEFAULT now()
)

一个功能

CREATE FUNCTION store_reward_transaction(bigint, text) returns record
    LANGUAGE plpgsql
    AS $_$
declare
_record record;
begin
update reward_transactions set count = count + 1, last_update = now() where uid = $1 and reward_type = $2::text returning count, last_update::timestamp with time zone into _record;
if found then
return _record;
end if;
begin
insert into reward_transactions (uid, count, reward_type) values ($1, 1, $2::text) returning count, last_update::timestamp with time zone into _record;
return _record;
exception when unique_violation then
update reward_transactions set count = count + 1, last_update = now() where uid = $1 and reward_type = $2::text returning count, last_update::timestamp with time zone into _record;
return _record;
end;
end
$_$;

问题是如何让这不返回rows: [ { store_reward_transaction: '(12,"2014-07-18 17:29:39.780207-05")' } ]而是返回类似于原始表中的select将具有的内容(即列名完整),我已经尝试将其作为tablename%ROWTYPE返回,使用自定义类型和其他一些东西,我似乎无法获得所需的输出。使用9.3.3

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT * FROM store_reward_transaction(4,'blah') f(count bigint, last_update timestamp with time zone);