Thanks a lot Geert. That looks great and should help.
Cheers :)
Main Topics
Browse All TopicsThanks for reading...
Never done a stored procedure before.
Here's the scenario:
On the server sits a table of seats available, example: A1, A2, A3, A4, A5, ... B1, B2, B3, B4, B5 etc.
Currently, for the system to choose a seat, it fetches all the seats in that table, sorts them in Ascending order, and chooses the first one that's available.
The problem is, what if two clients try to do this at the exact same time? They could hypothetically both choose the same seat.
So bottom line is I need our server to do this for me.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Geert - from what I know Firebird has a pretty good implementation of the SQL standard, except it likes all its table and column names in quotes, e.g. "TblBookings".
I think your problem is you're used to Oracle which seems to have its own deviation of the SQL standard.
But I understand, stored procedures may work in a special way in Firebird, but again, as I understand it Oracle has it's own stored procedure language.;
i have still a lot to learn about firebird/interbase
after tweaking and testing the procedure i ended up with this:
this still needs some performance upgrade:
first_free_seat = -1;
FOR
select seat_id
from seats
where ((taken = 0) or (taken is null))
and ((reservation_id) = 0 or (reservation_id is null))
order by seat_id asc
INTO :free_seat_id
DO
IF (first_free_seat = -1) THEN
first_free_seat = free_seat_id;
in oracle this would be
select seat_id INTO :free_seat_id
from seats
where ((taken = 0) or (taken is null))
and ((reservation_id) = 0 or (reservation_id is null))
and rownum < 2
order by seat_id asc
FOR
select seat_id
from seats
where ((taken = 0) or (taken is null))
and ((reservation_id) = 0 or (reservation_id is null))
order by seat_id asc
INTO :free_seat_id
DO
IF (first_free_seat = -1) THEN
first_free_seat = free_seat_id;
can become
select first 1 seat_id
from seats
where ((taken = 0) or (taken is null))
and ((reservation_id) = 0 or (reservation_id is null))
order by seat_id asc
INTO :first_free_seat;
if ("num_free_seats" = 0) then
begin
"Successful" = 'F';
end
ELSE /* if ("num_free_seats" > 0) then */
begin
SELECT FIRST 1 "ID" FROM "TblFlightScheduleSeats" WHERE ("PassengerID" is null AND "FlightID" = :"DaFlightID") INTO :"DaSeatID";
SELECT FIRST 1 "SeatNumber" FROM "TblFlightScheduleSeats" WHERE ("PassengerID" is null AND "FlightID" = :"DaFlightID") INTO :"DaSeatNumber";
UPDATE "TblFlightScheduleSeats" SET "PassengerID" = :"DaBookingID" WHERE "ID" = :"DaSeatID";
end
SUSPEND;
this will be more efficient
Business Accounts
Answer for Membership
by: Geert_GruwezPosted on 2009-07-23 at 22:50:54ID: 24932474
i dunno about firebird, but i have a similar problem
2 servers executing a list of tasks, 1 by 1
I use a repeat, select and update statement from delphi and a unique id for each server
table seats
seat_id: integer // number of seat
taken: integer (1 or 0) // seat free or not
reservation_id: integer // unique number per reservation
first find a free seat
this is a mix of pseudo and sql
Select allOpen in new window