You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL存储过程执行报错:operator does not exist: integer[] = integer

PostgreSQL存储过程报错:operator does not exist: integer[] = integer 排查

我尝试执行下面的PostgreSQL存储过程时,遇到报错:error: operator does not exist: integer[] = integer。调用存储过程时并没有传入数组,想确认存储过程写法是否有问题。

存储过程代码

CREATE OR REPLACE PROCEDURE public.parkingvehicle(
    IN p_dui text,
    IN p_desired_parking_id text,
    IN p_vehicle_no text,
    IN p_temp_id integer,
    OUT p_result integer)
LANGUAGE 'plpgsql'
AS
$BODY$
DECLARE
    user_id_val int;
    vehicle_no_val text;
BEGIN
    -- Execute the first query
    SELECT user_id, vehicle_no INTO user_id_val, vehicle_no_val
    FROM users
    WHERE dui = p_dui::int;

    -- Check if the user_id is null
    IF user_id_val IS NULL THEN
        -- Insert into temp_login table
        INSERT INTO temp_login(dui, vehicle_no, temp_id)
        VALUES (p_dui, p_vehicle_no, p_temp_id);

        -- Update parking table
        UPDATE parking 
        SET is_equipped = true, equipped_by = p_temp_id, equipped_time = now(), vehicle_no = p_vehicle_no
        WHERE parking_id = p_desired_parking_id;

        -- Set the result to 0 to indicate failure
        p_result := 0;
    ELSE
        -- Execute the second query
        UPDATE parking 
        SET is_equipped = true, equipped_by = user_id_val, equipped_time = now(), vehicle_no = vehicle_no_val
        WHERE parking_id = p_desired_parking_id;
        
        -- Set the result to 1 to indicate success
        p_result := 1;
    END IF;
END;
$BODY$

相关表结构

  • users表:字段包括user_id(整数类型)、dui(整数数组类型)、vehicle_no(文本类型)
  • temp_login表:字段包括dui(文本类型)、vehicle_no(文本类型)、temp_id(整数类型)

问题原因

报错的核心是users表的dui字段是整数数组类型,但存储过程里写了dui = p_dui::int——把文本参数转成单个整数后,直接和数组类型的字段做相等比较,PostgreSQL没有定义这种数组与单个整数直接相等的运算符,因此抛出错误。

修正方案

如果你的需求是判断p_dui转成的整数是否存在于dui数组中,需要改用PostgreSQL支持的数组操作方式:

方式1:使用数组包含运算符@>

SELECT user_id, vehicle_no INTO user_id_val, vehicle_no_val
FROM users
WHERE dui @> ARRAY[p_dui::int]::integer[];

方式2:使用ANY运算符

SELECT user_id, vehicle_no INTO user_id_val, vehicle_no_val
FROM users
WHERE p_dui::int = ANY(dui);

额外注意事项

  1. 要考虑SELECT ... INTO可能返回多行的情况,建议加上LIMIT 1,或者用SELECT ... INTO STRICT来明确处理无结果/多结果的场景,避免运行时抛出异常。
  2. 确保p_dui可以正常转换为整数,否则会出现类型转换错误,建议添加异常处理逻辑或者提前做参数校验。

内容的提问来源于stack exchange,提问作者Axen_Rangs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 00:07:41