PL/pgSQL 选择进入
在本教程中,您将学习如何使用 PL/pgSQL select into 语句从数据库中选择数据并将其分配给变量。
摘要:在本教程中,您将学习如何使用 PL/pgSQL select into 语句从数据库中选择数据并将其分配给变量。
PL/pgSQL Select Into 语句介绍
该 select into 语句允许您从数据库中选择数据并将数据分配给变量。
下面说明了该 select into 语句的语法:
select select_list
into variable_name
from table_expression;
在此语法中,您将变量放在 into 关键字之后。该 select into 语句会将 select 子句返回的数据分配给变量。
除了从表中选择数据外,您还可以使用 select 语句的其他子句,例如 join , group by, 和 having 。
PL/pgSQL Select Into 语句示例
请参阅以下示例:
do $$ declare
actor_count integer;
begin
-- select the number of actors from the actor table
select count(*)
into actor_count
from actor;
-- show the number of actors
raise notice 'The number of actors: %', actor_count;
end; $$
输出:
NOTICE: The number of actors: 200
在这个例子中:
- 首先,声明一个名为
actor_count的变量 ,用于存储actor表中的参与者数量。 - 其次,使用
select into语句将参与者的数量分配给actor_count. - 最后,显示一条消息,显示
actor_count使用该raise notice语句的变量的值。
概括
- 使用该
select into语句从数据库中选择数据并将其分配给变量。