【发布时间】:2017-03-22 13:11:14
【问题描述】:
如何在临时变量中获取一行并处理/使用其字段? 请参阅开始部分,我需要获取帐户信息,进行一些计算,例如我需要在 accountbalance 方法中获取 Account.field1 - Account.Field2 的值,该怎么做? --这是声明
PREPARE get_account (varchar) AS
SELECT * FROM "Accounts" WHERE "AccountKey" = $1 LIMIT 1;
-- Try to run directly
select EXECUTE(get_account("A200"));
--Created a function and used statement.
CREATE OR REPLACE FUNCTION accountbalance(VARCHAR) RETURNS REAL AS $$
DECLARE
AKey ALIAS FOR $1;
balance REAL;
account RECORD;
BEGIN
account := EXECUTE(get_account("A200"));
--Tried these too
--account := EXECUTE get_account('A200');
--account := EXECUTE get_account("A200");
--I need to get account data here, process, How to get data to a declared variable, how user specific column, May be something like Accounts."Total"..
--I tried to run direct query here and get data to account, but no success so tried prepared statement.
--I will be doing complex calculations here, trying to return a column for test , not sure is it correct?
RETURN account.Actual;
END;
$$ LANGUAGE plpgsql;
--Used function in sql
Select accountbalance('A200');
在这两种情况下都会收到这样的错误。
错误:“A200”列不存在 第 1 行:选择 EXECUTE(get_account("A200")); ^
**********错误**********
错误:“A200”列不存在 SQL 状态:42703 字符:28
【问题讨论】:
-
双引号用于关系,而不是值
-
select EXECUTE(get_account("A200"));时不执行准备好的语句 - 你执行你的函数 -
我尝试了所有选项,选择EXECUTE(get_account('A200')),然后选择EXECUTE(get_account(A200));
-
试试
execute get_account ('a200'); -
我可以运行这个 EXECUTE get_account('A200');它得到结果集,但在函数内它给出了语法错误。 account := EXECUTE get_account('A200');
标签: postgresql plpgsql