gpt4 book ai didi

sql - 函数语法错误

转载 作者:行者123 更新时间:2023-11-29 14:12:03 25 4
gpt4 key购买 nike

CREATE FUNCTION check_available_flightID(date)
RETURNS integer AS
$$
BEGIN
SELECT FlightID FROM Flight WHERE FlightDate::date = $1;

IF NEW.FlightID IS NULL THEN
RAISE EXCEPTION 'No filight in the date you enter'
END IF;

RETURN query SELECT FlightID FROM Flight WHERE FlightDate::date = $1;
$$
LANGUAGE SQL;

它说“IF”附近的语法错误......有点困惑为什么有些函数需要 BEGIN 而有些不需要。

谁能给我一个提示?

最佳答案

我不是 Postgres 专家,但我认为 new 用于触发器而不是常规函数。

我想你想要这样的东西:

CREATE FUNCTION check_available_flightID(date)
RETURNS integer AS
$$
declare newfid int;
BEGIN
SELECT FlightID into newfid FROM Flight WHERE FlightDate::date = $1;
IF newfid IS NULL THEN
RAISE EXCEPTION 'No flight in the date you enter';
END IF;
RETURN query SELECT FlightID FROM Flight WHERE FlightDate::date = $1;
end;
$$

关于sql - 函数语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15731247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com