简体   繁体   English

postgresql:将SQL查询的结果存储在变量中

[英]postgresql: store result of sql query in variable

I'm trying to store the result of a sql query, which should be a single date, in a variable. 我正在尝试将sql查询的结果存储在变量中,该查询应该是单个日期。 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 . 不幸的是,我在SELECT遇到语法错误。 Any idea what I'm doing wrong here or if this is even possible? 知道我在这里做错了什么,或者甚至有可能这样做吗?

Enclose the query in parenthesis. 将查询括在括号中。

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

Or use SELECT ... INTO . 或使用SELECT ... INTO

...
SELECT MIN(event_date) INTO date_ordered FROM event;
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM