1

How to get a row in a temp variable and process/use its fields?? See the begin section, I need to get account info, do some calculations, like I need to get value of Account.field1 - Account.Field2 in accountbalance method , how to do that?? --this is statement

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');

in both cases receive error like this.

ERROR: column "A200" does not exist LINE 1: select EXECUTE(get_account("A200")); ^

********** Error **********

ERROR: column "A200" does not exist SQL state: 42703 Character: 28

6
  • 1
    double quotes are used for relations, not values Commented Mar 22, 2017 at 13:18
  • you don't execute prepared statement when you select EXECUTE(get_account("A200")); - you execute your function Commented Mar 22, 2017 at 13:21
  • I tried all options, select EXECUTE(get_account('A200')), and select EXECUTE(get_account(A200)); Commented Mar 22, 2017 at 13:24
  • try execute get_account ('a200'); Commented Mar 22, 2017 at 13:25
  • Well I am able to run this EXECUTE get_account('A200'); and it gets result set, But within function it gives syntex error HERE. account := EXECUTE get_account('A200'); Commented Mar 22, 2017 at 13:28

2 Answers 2

1

you don't execute prepared statement when you select EXECUTE(get_account("A200")); - you execute your function. Here's example of how to run prepared statements:

t=# PREPARE get_account (varchar) AS SELECT * FROM pg_tables where tablename = $1;
PREPARE
t=# execute get_account ('pg_statistic');
 schemaname |  tablename   | tableowner | tablespace | hasindexes | hasrules | hastriggers
------------+--------------+------------+------------+------------+----------+-------------
 pg_catalog | pg_statistic | postgres   |            | t          | f        | f
(1 row)

For you prepared stmt it would be

execute get_account ('a200');

Regarding using sql EXECUTE in plpgsql block (which has it's own EXECUTE - very different from sql one), please read https://stackoverflow.com/a/12708117/5315974

Sign up to request clarification or add additional context in comments.

1 Comment

But this does not work within function accountbalance(), see my question, account := EXECUTE get_account('A200');
0

Why you need it? Any embedded SQL in PLpgSQL is implicitly prepared statement. Explicitly prepared statements are not supported in PLpgSQL. Maybe you want to use dynamic SQL - see EXECUTE statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.