Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Does my sp have to be that long ?

Author  Topic 

Pinto
Aged Yak Warrior

590 Posts

Posted - 2011-10-26 : 05:36:36
I have a table with a BookingNo(key) RoomId and a DateRequired, It has 28 time period fields P1 to P28

My user selects a room and date - they then select a start time and end time eg P8 and P12. How can I write an sp that basically does this for any choice eg the next choice may be the same room and date P18 to P22

Select * from MyTable where
RoomId = @RoomId
DateRequired = @DateRequired and

P8 = @P8
P9 = @P9
P10 = @P10
P11 = @P11
P12 = @P12

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-10-26 : 05:53:04
Normalise it.

Have a table for rooms -- Each room has a unique key and data about the room
have a table for timeperiods -- each timeperiod has a unique key and data about that timeperiod
have a table for dates -- each data has a unique key and data about the date
have a table for bookingtimeperiods -- expand this table to have all possible combinations of timeperiods
Have a table for bookings -- bookings have a unique key and foreign keys to rooms, dates, bookingtimeperiods


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-10-26 : 06:00:13
Sorry - once you've normalised it then the stored proc should be pretty trivial.

Look up the keys based on the parameters passed in and then just write a booking entry.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2011-10-26 : 06:01:40
I have all those tables
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-10-26 : 07:04:05
You said:
quote:

I have a table with a BookingNo(key) RoomId and a DateRequired, It has 28 time period fields P1 to P28


That doesn't sound like a normalised design. That table sounds like a spreadsheet.

P18, 22 etc are the time periods?

Each time period corresponds to a particular start time and end time?

Then you could create a table that has all the possible combinations of periods (probably not that many) and have 1 key that represents each possible combination.

Then when you get your start and end periods as parameters

you just look up the key for them and then insert a booking entry

for

roomNo \ Date \ TimePeriodKey \ Person......

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-10-26 : 09:41:49
create a view

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -