我试图在 postgresql sql 脚本中声明默认值为 0 的整数变量:
DECLARE user_id integer;
但它返回异常:
ERROR: syntax error at or near "integer"
我不确定如何声明一个变量,然后在 while 循环中使用这个变量。
最佳答案
您必须将代码放在用户定义的函数中。它不适用于 sql 窗口。下面的示例是一个返回您发送的号码的函数。变量
--mybase is your database name. If public, remove it.
CREATE OR REPLACE FUNCTION mybase.my_new_rotine(numeric)
RETURNS numeric AS
$BODY$
--here, get the first variable from function
declare id numeric= $1;
begin
--return the number
return id;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
然后,你可以在sql窗口中使用它,像这样:
select * from mybase.my_new_rotine(1)
将返回 1
关于PostgreSql - 声明整型变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40771647/