我正在PostgreSQL中编写一个带有SQL函数的程序。该程序的目的是接收两个输入,并在功能上返回一个输出。我有一个表,即“分数”,其中3列1)quid(整数类型)2)sca(整数类型)3)scb(整数类型)。我发送两个输入 quid 和得分。在该内部函数之后,将两个输入初始化为新变量。问题是我无法将选择查询加载到新变量并返回它。当我发送两个变量时,它给出了错误。我使用以下方法发送变量: select sendans(2,'b'); 我的源代码在这里:
create or replace function sendans(n integer,m text)
returns integer as $$
declare
num1 integer=n;
num2 text=m;
total integer=0;
begin
if num2='a' then
select total = sca from score where quid=num1;
return total;
end if;
if num2='b' then
select total = scb from score where quid=num1;
return total;
end if;
end;
$$ language plpgsql;
答案 0 :(得分:1)
要将查询中的列值分配给PLpgSQL中的变量,必须使用SELECT <column> INTO <variable>
语法。例如
SELECT sca INTO total
FROM score
WHERE quid = num1;