Search code examples
firebirdfirebird-3.0firebird-psql

procedure return True or False


I need a stored procedure in firebird 3.0 to return True, False or 1,0 result to check if this person or the record exists in database. If exists the procedure should return true, if not return False, or return 1, if not return 0

This is my procedure but it gives me an error: https://i.ibb.co/HLZQY59/Capture.jpg

Invalid token.
Dynamic SQL Error.
SQL error code = -104
Token unknown - line 10, column 1.
end.

I want to check if the record is exist or not by checking the first name last name and day of birth.

create procedure aa(
v varchar(20),
g varchar(20),
dd date)
as 
begin
select fname,lname,bday from STUDENT
where not exists (select fname,lname,bday from STUDENT  where fname=:v and lname=:g and bday=:dd)
end

Solution

  • Your stored procedure doesn't work because 1) it doesn't have a RETURNS clause so it cannot return true or false, and 2) a select in PSQL requires an INTO clause to put the values into a variable or return-parameter.

    Based on your code, a stored procedure that does what you want would be:

    create procedure aa(
      v varchar(20),
      g varchar(20),
      dd date)
      returns (student_exists boolean)
    as 
    begin
      student_exists = exists (select * from STUDENT where fname=:v and lname=:g and bday=:dd);
    end
    

    Depending on what you're need to achieve, a function might be more appropriate:

    create function aa(
      v varchar(20),
      g varchar(20),
      dd date)
      returns boolean
    as 
    begin
      return exists (select * from STUDENT where fname=:v and lname=:g and bday=:dd);
    end