🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.interbase.sql
Article #180

How to do this selection?

#180
From: Johan Anderlund
Date: Sat, 12 Jul 2008 17:33
87 lines
2821 bytes
Hi,

I have huge problems figuring out how to write my stored procedure, 
which will return all times in a day (SELDAY) a staff (ID_STAFF) is free 
after a selected time (START_TIME). Free is the same as not having 
anything booked in my application. STIME is the length of the selected 
treatment.

I want it too return something like this:

32
33
36
37
38
42
...

The time is numbers of quarters, so 32 is the same as 32 / 4 = 8:00.

The problem is when a staff has something booked, lets say for time 32
and 33. I will then get this error: multiple rows in singleton select.

The select statement in my while loop (select BD.ID_BOOKING) will return 
the same ID_BOOKING both when I is 32 as when I is 33.

This is the stored procedure:

ALTER PROCEDURE "WEB_FREETIMES"
(
   "ID_SERVICE" INTEGER,
   "SELDAY" DATE,
   "START_TIME" INTEGER,
   "ID_STAFF" INTEGER
)
RETURNS
(
   "FREE_TIME" INTEGER
)
AS
DECLARE VARIABLE "SERVICE_TIME" INTEGER;
DECLARE VARIABLE "I" INTEGER;
DECLARE VARIABLE "NCOUNT" INTEGER;
DECLARE VARIABLE "OPENING_END" INTEGER;
DECLARE VARIABLE "SHIFT_END" INTEGER;
BEGIN
   select STIME
   from SERVICE
   where ID_SERVICE = :ID_SERVICE
   into :SERVICE_TIME;

   select SHIFT_END
   from SHIFT
   where ID_STAFF = :ID_STAFF
   and SHIFT_DATE = :SELDAY
   into :SHIFT_END;

   if (SHIFT_END is null) then
     SHIFT_END = 0;

   I = START_TIME;
   WHILE (I <= SHIFT_END) DO
   BEGIN
     IDBOOKING = 0;

     select BD.ID_BOOKING
     from BOOKING_DETAIL BD
     left join BOOKING B on (BD.ID_BOOKING = B.ID_BOOKING)
     where BD.ID_STAFF = :ID_STAFF
     and B.BOOKING_DATE = :SELDAY
     and (BD.TIME_BEGIN between :I and :SERVICE_TIME + :I - 1
       or BD.TIME_BEGIN + BD.TIME_LENGTH - 1 between :I and 
:SERVICE_TIME + :I - 1)
     INTO :IDBOOKING;

     IF (!IDBOOKING > 0) THEN
     BEGIN
       FREE_TIME = :I;
       SUSPEND;
     END
     I = I + 1;
   END
END;

Anyone can help me?

// Johan
Technical Details
Message-ID: <4878eaf0$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!feed.xsnews.nl!border-2.ams.xsnews.nl!news.isoc.lu!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,1207fff9d2f6d452,start
X-Google-Attributes: gid114a0e,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Sat, 12 Jul 2008 19:33:33 +0200
From: Johan Anderlund <johan@anderlund.net>
User-Agent: Thunderbird 2.0.0.14 (Windows/20080421)
MIME-Version: 1.0
Newsgroups: borland.public.interbase.sql
Subject: How to do this selection?
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 84.55.83.114
Message-ID: <4878eaf0$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1215884016 84.55.83.114 (12 Jul 2008 10:33:36 -0700)
Lines: 87
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!feed.xsnews.nl!border-2.ams.xsnews.nl!news.isoc.lu!newsgroups.borland.com!not-for-mail