Postgresql 为什么我的plpgsql函数不返回行
当对一个用户独立运行这两个查询时,我得到的结果是300和0,但是在我的函数中没有返回任何行 我正在从SQL Server迁移到PostgresSQL,所以有些东西对我来说有点陌生Postgresql 为什么我的plpgsql函数不返回行,postgresql,plpgsql,postgresql-9.4,Postgresql,Plpgsql,Postgresql 9.4,当对一个用户独立运行这两个查询时,我得到的结果是300和0,但是在我的函数中没有返回任何行 我正在从SQL Server迁移到PostgresSQL,所以有些东西对我来说有点陌生 create or replace function getsummary(userid int) returns table (assetTotal numeric, liabilityTotal numeric) as $$ BEGIN SELECT sum(t.credit) - sum(t.debit) in
create or replace function getsummary(userid int)
returns table (assetTotal numeric, liabilityTotal numeric)
as $$
BEGIN
SELECT sum(t.credit) - sum(t.debit)
into assetTotal
from transactions t
join user_institutes i on t.user_institute_id = i.id
where i.account_type in (1,3,4,5)
and i.user_id = userid;
select sum(t.credit) - sum(t.debit)
into liabilityTotal
from transactions t
join user_institutes i on t.user_institute_id = i.id
where i.account_type in (2,8,10)
and i.user_id = userid;
END
$$ language plpgsql;
select * from getsummary(1)
非常感谢没有深入研究您的问题,只是指出缺少的部分。试一下
create or replace function getsummary(userid int)
returns table (assetTotal numeric, liabilityTotal numeric)
as $$
DECLARE
_assetTotal numeric;
_liabilityTotal numeric;
BEGIN
SELECT sum(t.credit) - sum(t.debit)
into _assetTotal
from transactions t
join user_institutes i on t.user_institute_id = i.id
where i.account_type in (1,3,4,5)
and i.user_id = userid;
select sum(t.credit) - sum(t.debit)
into _liabilityTotal
from transactions t
join user_institutes i on t.user_institute_id = i.id
where i.account_type in (2,8,10)
and i.user_id = userid;
return query select _assetTotal, _liabilityTotal;
END
$$ language plpgsql;
它可能还说,没有任何回报就到达了?。。那是因为你没有任何外部的争论,也没有返回任何东西。。。您是否实际插入到assetTotal表?。@VaoTsun,没有,只是说没有返回行和执行时间。因此,我错误地认为将x插入x填充我的表?现在我在想,我是否想以某种方式插入一行并将其插入表中以返回?哦-我以为OP的意思是他正在从t-SQL移动到plpgsql:)@a_horse_,没有名字。我的意思是,我以前没有使用过postgres数据库,我的知识完全掌握在mssql/mysql中。我不知道如何将sql存储过程转换为postgresql函数。谢谢!我不得不将声明的类型更改为numeric,但这成功了!