1

How to store multiple variables from the query in the stored procedure?

For one variable it can be done easily but how to do it if it's more than one for the same query?

declare num1 int;
declare num2 int;
select number1 into num1 from table_a where id = 1;

-- This one is not correct
select number1 into num1, number2 into num2 from table_a where id = 1;

Is there any simple way to do it without using cursor variable?

1
  • I prefer to use record variable in such cases. Commented Oct 24, 2014 at 13:22

1 Answer 1

4

Quote from the manual:

where target can be a record variable, a row variable, or a comma-separated list of simple variables

So it should be:

select number1, number2 
   into num1, num2 
from table_a where id = 1;

Alternatively you can use a record variable:

declare result_rec record;
...

select number1, number2 
    into result_rec
from table_a where id = 1;
Sign up to request clarification or add additional context in comments.

1 Comment

With record, it looks better (IMHO) to do FOR result_rec IN SELECT * FROM table_a WHERE id=1 LOOP ... END LOOP

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.