【问题标题】:argument of AND must be type boolean, not type integer in postgresqlAND 的参数必须是布尔类型,而不是 postgresql 中的整数类型
【发布时间】:2021-09-09 21:43:20
【问题描述】:

这是我做的触发功能。我在这里要做的是,如果 test_tx_out 表有变化,就会触发触发函数。一个变量被声明为布尔值,它存储真或假。然后,我使用select exists(...) 查询检查 test_address 表中是否已经存在地址并将其存储在 boolAddress 中。 if 条件是如果new.stake_address_id AND boolAddress 为真,则执行内部语句,否则执行另一个 else 语句块,但我收到错误argument of AND must be type boolean, not type integer in postgresql。如何将 new.stake_address_id 转换为布尔值?好吧,我想如果new.stake_address_id 不为空或不为空,那将是真的。基本上,如果有值,我希望 new.stake_address_id 为 true,否则为 false。

CREATE OR REPLACE FUNCTION test_stake_addresses_tx_out_trigger()
    RETURNS trigger AS $$
DECLARE
    boolAddress BOOLEAN;
BEGIN
    select exists (SELECT 1 FROM test_address where address = new.address) into boolAddress;
    IF new.stake_address_id AND boolAddress THEN
        UPDATE test_address
        SET amount = amount + new.value
        WHERE NEW.stake_address_id = test_address.id;
        RETURN NEW;
    ELSE
        INSERT INTO test_address(id,address,amount)
        VALUES(new.stake_address_id,new.address,new.value);
        RETURN NEW;
    END IF;
end;
$$
    LANGUAGE 'plpgsql';

CREATE TRIGGER test1
    AFTER INSERT
    ON "test_tx_out"
    FOR EACH ROW
EXECUTE PROCEDURE test_stake_addresses_tx_out_trigger();

【问题讨论】:

    标签: postgresql triggers


    【解决方案1】:

    好吧,我想如果 new.stake_address_id 不为空或为空,

    整数值不是布尔值(它只能为空,不存在“空”整数)

    如果你想要一个带有整数的布尔表达式,你需要将它与某个东西进行比较。如果要测试它是否为空,请使用is not null

    if new.stake_address_id is not null and boolAddress then
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 2023-01-13
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多