错误:运算符不存在:整数 == 整数

Raj*_*air 8 sql postgresql

我在 postgres 函数中使用这些语句。

Select count(*) into V_check
from employee
where employee_name like 'Raj%';

if V_check == 0
then
     update exception set exception_found = 'Raj';
end if;
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

ERROR:  operator does not exist: integer == integer
LINE 1: SELECT V_check == 0
Run Code Online (Sandbox Code Playgroud)

All*_*lan 8

你应该使用=而不是==.

以下是您可以使用的不同比较运算符的列表:

Operator    Description
=   Equal
<>  Not equal. Note: In some versions of SQL this operator may be written as !=
>   Greater than
<   Less than
>=  Greater than or equal
<=  Less than or equal
BETWEEN Between an inclusive range
LIKE    Search for a pattern
IN  To specify multiple possible values for a column
Run Code Online (Sandbox Code Playgroud)


Gor*_*off 2

正如所指出的,相等的比较运算符=不是==。但是,您应该将条件写为:

if not exists (select 1 from employee where employee_name like 'Raj%')
then
     update exception
         set exception_found = 'Raj';
end if;
Run Code Online (Sandbox Code Playgroud)

这可以为您节省一份声明。此外,比--not exists更快,因为可以停在第一个匹配行。count(*)not exists

或者完全放弃条件:

update exception 
    set exception_found = 'Raj'
    where not exists (select 1 from employee where employee_name like 'Raj%');
Run Code Online (Sandbox Code Playgroud)