2017-03-22 3 views
0

一時変数で行を取得し、そのフィールドを処理/使用する方法はありますか? 私はaccount.field1 - Account.Field2のaccountbalanceメソッドの値を取得する必要があるように、開始セクションを参照して、私は、アカウント情報を取得する必要がありますいくつかの計算を行うには、それを行う方法?? - これはステートメントですポストグルでプリペアドステートメント/関数を実行できません

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"))を選択します。 ^

********** **********エラー

ERROR:コラム "A200" SQL状態は存在しません:42703 文字:28

+1

二重引用符は値ではなく、値ではありません –

+0

EXECUTE(get_account( "A200"));を選択したときにプリペアドステートメントを実行しません - 関数を実行します –

+0

すべてのオプションを試しましたが、EXECUTE(get_account ( 'A200'))を選択し、EXECUTE(get_account(A200))を選択します。 –

答えて

0

なぜ必要なのですか? PLpgSQLの埋め込みSQLは暗黙的に準備された文です。明示的に準備された文は、PLpgSQLではサポートされていません。動的SQLを使用したいと思うかもしれません - EXECUTEステートメントを参照してください。

1

EXECUTE(get_account( "A200"))を選択するとprepared statementを実行しません。あなたの機能を実行します。ここでプリペアドステートメントを実行する方法の例です:

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) 

あなたのために準備されたのstmtは、それがplpgsqlがブロック内のSQL EXECUTEを使用してについて

execute get_account ('a200'); 

だろう(それはEXECUTE自身だ持っている - SQLの1と非常に異なります)お読みくださいhttps://stackoverflow.com/a/12708117/5315974

+0

しかし、これは機能accountbalance()内では、私の質問を参照してください アカウント:= EXECUTE get_account( 'A200'); –

+0

詳細が更新されました –

関連する問題