Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数错误–;postgresql_Postgresql_Function_Triggers - Fatal编程技术网

函数错误–;postgresql

函数错误–;postgresql,postgresql,function,triggers,Postgresql,Function,Triggers,我创建了这个函数来检查是否正确计算了一次飞行的总成本。它应该将座位的价格(例如400)乘以客户要求的座位数量(例如2)。如果客户正确输入总数,例如800,则应成功返回查询。如果他们没有正确输入总额,例如1000,则应增加“价格计算不正确”的例外情况 CREATE FUNCTION check_totalcost() RETURNS trigger AS $check_totalcost$ DECLARE seatprice int; DECLARE noofseats int;

我创建了这个函数来检查是否正确计算了一次飞行的总成本。它应该将座位的价格(例如400)乘以客户要求的座位数量(例如2)。如果客户正确输入总数,例如800,则应成功返回查询。如果他们没有正确输入总额,例如1000,则应增加“价格计算不正确”的例外情况

CREATE FUNCTION check_totalcost() RETURNS trigger AS $check_totalcost$
    DECLARE seatprice int;
    DECLARE noofseats int;
    BEGIN
        SELECT priceperseat INTO seatprice
        FROM flight
        WHERE flightid = flightid;

        SELECT numseats INTO noofseats
        FROM flightbooking
        WHERE flightid = flightid;

        IF(seatprice*noofseats != new.totalcost) THEN
        RAISE EXCEPTION 'price has been calculated incorrectly';

        END IF;
        RETURN NEW;
    END;
    $check_totalcost$ LANGUAGE plpgsql;
    /* This is used to run the function above */
    CREATE TRIGGER insert_totalcost
    BEFORE INSERT OR UPDATE ON flightbooking
    FOR EACH ROW EXECUTE PROCEDURE check_totalcost();
当我输入的总数不正确时,它会引发异常,这正是我想要的,但当我正确输入总数时,当它应该成功返回查询时,它也会引发异常。

查询

SELECT priceperseat INTO seatprice
FROM flight
WHERE flightid = flightid;

SELECT numseats INTO noofseats
FROM flightbooking
WHERE flightid = flightid;  
没有什么意义,因为条件总是正确的。 我猜您想从插入/更新的记录中获取
flightid
。使用特殊记录
新建

WHERE flightid = NEW.flightid;  

函数中还有一个逻辑错误。座位数也应从新的
记录中获取

CREATE OR REPLACE FUNCTION check_totalcost() 
RETURNS trigger AS $check_totalcost$
    DECLARE seatprice int;
    BEGIN
        SELECT priceperseat INTO seatprice
        FROM flight
        WHERE flightid = new.flightid;

        IF seatprice * new.numseats != new.totalcost THEN
            RAISE EXCEPTION 'price has been calculated incorrectly';

        END IF;
        RETURN NEW;
    END;
    $check_totalcost$ LANGUAGE plpgsql;
疑问

SELECT priceperseat INTO seatprice
FROM flight
WHERE flightid = flightid;

SELECT numseats INTO noofseats
FROM flightbooking
WHERE flightid = flightid;  
没有什么意义,因为条件总是正确的。 我猜您想从插入/更新的记录中获取
flightid
。使用特殊记录
新建

WHERE flightid = NEW.flightid;  

函数中还有一个逻辑错误。座位数也应从新的
记录中获取

CREATE OR REPLACE FUNCTION check_totalcost() 
RETURNS trigger AS $check_totalcost$
    DECLARE seatprice int;
    BEGIN
        SELECT priceperseat INTO seatprice
        FROM flight
        WHERE flightid = new.flightid;

        IF seatprice * new.numseats != new.totalcost THEN
            RAISE EXCEPTION 'price has been calculated incorrectly';

        END IF;
        RETURN NEW;
    END;
    $check_totalcost$ LANGUAGE plpgsql;

当我在flightid前面放置new时,我的触发器总体上不起作用。当我插入一个总成本错误的flightbooking时,它会插入它非常感谢你,这很有效,也很有帮助。当我在flightid前面插入新的时,我的触发器整体上不起作用。当我插入一个总成本错误的航班预订时,它会插入它,非常感谢,这很有效,非常有用。