为什么我的plpgsql函数没有返回任何行(why does my plpgsql function return no rows)
当两个查询针对用户独立运行时,我得到结果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) 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)
非常感谢
when the two queries are run independently against a user I get the results 300 and 0 however in my function I am getting no rows returned.
I am moving from SQL Server to PostgresSQL so some things are a bit alien to me!
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)
Many thanks
原文:https://stackoverflow.com/questions/43824847
更新时间:2022-12-05 09:12
最满意答案
没有深入查询您的查询,只是指出缺少部分。 尝试?:
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;
not looking deep to your queries, just pointing missing part. try?:
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;
相关问答
更多-
传递空输入参数时,PLPGSQL函数不返回任何结果(PLPGSQL Function returns no results when I pass a null input parameter)[2021-12-03]
您正在传递一个空字符串,它不是null,如mfgpart。 所以第一个查询将被执行。 仅当您将null作为mfgpart传递时,才会执行第二个查询。 如果要在传递空字符串时执行第二个查询,请更改 IF V_mfgpart IS NOT NULL OR V_mfgpart <> ' ' THEN 至 IF V_mfgpart IS NOT NULL and V_mfgpart <> '' THEN 注意空格与空字符串 我怀疑你想要这个 create or replace function iml.udf_ ... -
所有这些工作: 选项1: CREATE OR REPLACE FUNCTION func2(mode integer) RETURNS integer AS $BODY$ DECLARE _result integer; BEGIN _result = 2; IF mode=1 THEN _result = func1(); END IF; --more stuff ..... RETURN _result; END $BO ...
-
pllua可以做plpgsql的RETURN TABLE吗?(Is it possible for pllua to do the same thing as RETURN TABLE of plpgsql?)[2021-10-04]
我期待回归测试,这是不可能的。 但plpgsq RETURN QUERY是“宏”的 FOR r IN SELECT ... LOOP RETURN NEXT r; END LOOP; 这在PLlua中是可能的 I looked to regress test and it is not possible. But plpgsq RETURN QUERY is "macro" for FOR r IN SELECT ... LOOP RETURN NEXT r; END LOOP; And it ... -
ResultSet result; result=Pstatement.executeQuery("Select takeSomething()"); result.next(); System.out.println(result.getString(1)); 与其他所有选择语句一样。 BY - > a_horse_with_no_name ResultSet result; result=Pstatement.executeQuery("Select takeSomething()"); result ...
-
https://www.postgresql.org/docs/current/static/plpgsql-control-structures.html : FOR-IN-EXECUTE语句是迭代行的另一种方式: t=# do $$ declare _t text := 'pg_tables'; _r record; begin for _r in execute format('select * from %I limit 4',_t) loop raise info '%',_r ...
-
有更多的方法可以做到这一点。 一: postgres=# SELECT * FROM boo; id | foo_id | a | b ----+--------+-----+------ 1 | 1 | 100 | 1000 2 | 1 | 200 | 2000 3 | 2 | 300 | 4000 (3 rows) postgres=# SELECT * FROM foo; id | a | b ----+---+--- 1 | 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 ...
-
CREATE FUNCTION test() RETURNS my_table AS $BODY$ DECLARE q4 my_table; BEGIN -- add brackets to get a value -- select row as one value, as q4 is of the type my_table -- and limit result to one row q4 := (SELECT my_table FROM my_table ...
-
使用IN运算符: ... if old.status IN (5,6,4,7) then do something. end if; ... Use IN operator: ... if old.status IN (5,6,4,7) then do something. end if; ...
-
多次运行后,plpgsql函数中的Postgresql查询速度变慢(Postgresql query in plpgsql function slows down after several runs)[2022-04-02]
由于查询/优化器更喜欢array_b的文字,我切换到动态sql,并将变量作为文字输入: return query execute format( 'with data as ( -- this part of the query returns ~5000 rows and 40 columns select d.* from public.dashboard_data_view d -- ~10 million rows in this view where d. ...