1

This is my code:

amount INTEGER;

amount := select count(*) from moneyTable;

I'm getting the following error:

ERROR:   syntax error at or near "select"

Can someone help me out.

2
  • What do you want to achieve? Commented Nov 18, 2015 at 1:03
  • @MrsEd im trying to store the number of tuples in the variable amount. Then use that variable later for other processing. But currently im getting that syntax error and I have no idea why this is happening. Commented Nov 18, 2015 at 1:25

2 Answers 2

2

From the fine manual:

An assignment of a value to a PL/pgSQL variable is written as:

variable { := | = } expression;

but select ... isn't an expression. If you want to assign values from a SELECT to variables, you want to use INTO:

select count(*) into amount from moneyTable;
--              ^^^^^^^^^^^
Sign up to request clarification or add additional context in comments.

4 Comments

Now im getting the error ERROR: syntax error at or near " * ". CONTEXT: invalid type name "count(*) into amount from moneyTable"
Should work fine (sqlfiddle.com/#!15/6558d/1 (sorry about the function formatting, sqlfiddle has problems with multi-line dollar-quoted strings it seems)). You're doing this inside a function, right?
I have it in the declaration part of my function
You should have that assignment in the body of the function. A bit of time with the PL/pgSQL documentation might be useful as well.
1

Either the @mu is to short answer or this

amount := (select count(*) from moneyTable);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.