1

I am writing a postgres function where I have declared a variable called fruit.

 $$
    DECLARE
      IN_fruit varchar;
      sql;
    BEGIN
        IN_fruit := EXECUTE 'SELECT fruit FROM fruits WHERE id = 1'

How can I get this work in a way that the result returned from this query is stored in the variable IN_fruit, which I can use for later use in my main SQL query.

1 Answer 1

1

Use the code below

SELECT INTO IN_fruit  fruit FROM fruits WHERE id = 1

Reference here http://www.postgresql.org/docs/8.0/static/plpgsql-statements.html#PLPGSQL-SELECT-INTO

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

2 Comments

Thanks. It worked. This can also be written as: SELECT fruit INTO IN_fruit FROM fruits WHERE id = 1;
Yup. Your way is more logical because fruit "will be saved" into IN_fruit :).

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.