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;   

it said that the syntax error near "IF" ... I was searching the syntax for the function... A bit confuse why some of function need the BEGIN and some are not..

Can anyone please give me some hint..thanks so much..!

share|improve this question
Is this supposed to be a trigger function? If yes, provide the trigger creation script, some explanation and the table definition of involved tables. Please clarify. – Erwin Brandstetter 48 mins ago

2 Answers

up vote 0 down vote accepted

I am not an expert in Postgres, but I think new is for triggers and not for regular functions.

I think you want something like:

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;
$$
share|improve this answer
thanks but it said Syntax error near END IF; – johnnily 41 mins ago
@johnnily: That was because of the missing ; after the raise statement. There are more syntax errors here. But the guessing game is pointless. Please edit your question and provide more information as requested. – Erwin Brandstetter 36 mins ago
@johnnily . . . I just added a semicolon to the end of the statement, which I think is necessary in Postgres. – Gordon Linoff 36 mins ago
it work until i change the returns INTEGER to SETOF...thanks – johnnily 7 mins ago

Among other syntax errors, you are declaring it to be an SQL function, while in fact you are using PL/pgSQL syntax. Make that:

LANGUAGE plpgsql
share|improve this answer
thanks.but still the error occupied.. – johnnily 37 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.