PL/pgSQL IF 语句

在本教程中,您将学习如何使用 PL/pgSQL if 语句根据特定条件执行命令。

摘要:在本教程中,您将学习如何使用 PL/pgSQL if 语句根据特定条件执行命令。

该 if 语句根据布尔表达式的结果确定要执行的语句。

PL/pgSQL 为您提供了三种形式的 if 语句。

  • if then
  • if then else
  • if then elsif

1) PL/pgSQL if-then 语句

下面说明了该 if 语句的最简单形式:

if condition then
   statements;
end if;

如果 a为真,则 if 执行该语句。如果计算结果为,则控制将传递到部件之后的下一个语句。 statements condition condition false END if

该 condition 是一个布尔表达式计算结果为真或假。

The statements 可以是一个或多个语句,如果 condition 为真,则将执行这些语句。它可以是任何有效的语句,甚至是另一个 if 语句。

当一个 if 语句被放置在另一个 if 语句中时,它被称为嵌套 if 语句。

下面的流程图说明了这个简单的 if 语句。

PL/pgSQL if 语句

请参阅以下示例:

do $$ declare
  selected_film film%rowtype;
  input_film_id film.film_id%type := 0;
begin  

  select * from film
  into selected_film
  where film_id = input_film_id;

  if not found then
     raise notice'The film % could not be found', 
        input_film_id;
  end if;
end $$

在此示例中,我们通过特定电影 ID ( 0)选择了一部电影。

该 found 是一个全局变量是在PL / pgSQL的过程语言可用。If the [select into](https://www.postgresqltutorial.com/plpgsql-select-into/) statement set the found variable if a row已分配或未 false 返回任何行。

我们使用该 if 语句来检查 id (0) 的电影是否存在,如果不存在则发出通知。

ERROR:  The film 0 could not be found

如果您将 input_film_id 变量的值更改为电影表中存在的某个值,例如 100,您将看不到任何消息。

2) PL/pgSQL if-then-else 语句

下面说明了该 if-then-else 语句的语法:

if condition then
  statements;
else
  alternative-statements;
END if;

该 if then else 语句执行中的语句, if 如果分支 condition 评估为True; 否则,它执行 else 分支中的语句。

以下流程图说明了该 if else 语句。

PL/pgSQL if else 语句

请参阅以下示例:

do $$ declare
  selected_film film%rowtype;
  input_film_id film.film_id%type := 100;
begin  

  select * from film
  into selected_film
  where film_id = input_film_id;

  if not found then
     raise notice 'The film % could not be found', 
        input_film_id;
  else
     raise notice 'The film title is %', selected_film.title;
  end if;
end $$

在此示例中,电影 id 100 存在于电影表中,因此 FOUND 变量设置为 true。因此, else 分支中的语句被执行。

这是输出:

NOTICE:  The film title is Brooklyn Desert

3) PL/pgSQL if-then-elsif 语句

下面说明了该 if then elsif 语句的语法:

if condition_1 then
  statement_1;
elsif condition_2 then
  statement_2
...
elsif condition_n then
  statement_n;
else
  else-statement;
end if;

该 if 和 ifthen else 语句评估一个条件。但是,该 if then elsif 语句评估多个条件。

如果 a condition 为真,则执行该分支中的相应语句。

例如,如果 condition_1 是, true 则 if then ELSif 执行 statement_1 并停止评估其他条件。

如果所有条件评估为 false ,则 if then elsif 执行 else 分支中的语句。

以下流程图说明了该 if then elsif 语句:

PL/pgSQL if ELSif else 语句

让我们看看下面的例子:

do $$ declare
   v_film film%rowtype;
   len_description varchar(100);
begin  

  select * from film
  into v_film
  where film_id = 100;

  if not found then
     raise notice 'Film not found';
  else
      if v_film.length >0 and v_film.length <= 50 then
         len_description := 'Short';
      elsif v_film.length > 50 and v_film.length < 120 then
         len_description := 'Medium';
      elsif v_film.length > 120 then
         len_description := 'Long';
      else 
         len_description := 'N/A';
      end if;

      raise notice 'The % film is %.',
         v_film.title,  
         len_description;
  end if;
end $$

这个怎么运作:

  • 首先,选择id为100的电影。如果电影不存在,则提出未找到电影的通知。
  • 其次,使用 if then elsif 语句根据影片的长度为影片指定一个描述。

概括

  • if then 当条件为 时,使用来执行语句 true 。
  • 使用 if then else 以在条件为 时执行语句, true 并在条件为 时执行其他语句 false 。
  • 使用 if then elsif 来评估多个条件并在相应条件为 时执行语句 true 。