1

I'm trying to store the result of a sql query, which should be a single date, in a variable. I'm doing this because I plan to reuse the variable several times throughout the rest of the script. I've tried the following:

DO
$$
DECLARE
  date_ordered date;
BEGIN
  date_ordered := SELECT MIN(event_date) FROM event;
END;
$$

Unfortunately I'm getting a syntax error at SELECT. Any idea what I'm doing wrong here or if this is even possible?

1 Answer 1

3

Enclose the query in parenthesis.

...
date_ordered := (SELECT MIN(event_date) FROM event);
...

Or use SELECT ... INTO.

...
SELECT MIN(event_date) INTO date_ordered FROM event;
...
Sign up to request clarification or add additional context in comments.

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.