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.
| Author |
Topic |
|
DavisK
Starting Member
6 Posts |
Posted - 2005-11-23 : 10:23:50
|
| Background: I have a reservations database that cannot have duplicate reservations for the same piece of equipment at the same time. Two main tables are involved; the first one (PR_Add_Record) is where the end-user enters the request. This record is then compared to the historical table (PR_Reservations) to check for conflicts. If there is a conflict then a message should print indicating a conflict; if no conflict exists then the record from PR_Add_Record should be appended to the PR_Reservations and the PR_Add_Record table should be cleared.The following code will select records from the PR_Reservations table appropriately but nothing seems to happen after that; the code seems to stop at the "If @@ROWCount = 0". What's wrong?CREATE PROCEDURE [dbo].[spInsertRows] ASSelect * from PR_Add_Record, PR_ReservationsWhere PR_Add_Record.resource_id = PR_Reservations.resource_idand PR_Add_Record.start_time Between PR_Reservations.start_time and PR_Reservations.end_timeor PR_Add_Record.end_time Between PR_Reservations.start_time and PR_Reservations.end_timeIf @@RowCount <>0Print 'Sorry, that piece of equipment has already been reserved within the time frame requested.'Else Insert into dbo.PR_Reservations (resource_id, location, username, start_time, end_time) SELECT resource_id, location, username, start_time, end_timeFrom dbo.PR_Add_RecordDelete from dbo.PR_Add_RecordGOThanks |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-11-23 : 10:36:42
|
| if it selects something you should get executedIf @@RowCount <>0Print 'Sorry, that piece of equipment has already been reserved within the time frame requested.'Delete from dbo.PR_Add_RecordMaybe it's getting blocked trying to deliver the resultset from the select?Are you sure you don't want something more likeif exists (Select * from PR_Add_Record, PR_ReservationsWhere PR_Add_Record.resource_id = PR_Reservations.resource_idand (PR_Add_Record.start_time Between PR_Reservations.start_time and PR_Reservations.end_timeor PR_Add_Record.end_time BetweenPR_Reservations.start_time and PR_Reservations.end_time))beginInsert into dbo.PR_Reservations (resource_id,location,username,start_time,end_time)SELECTresource_id,location,username,start_time,end_timeFrom dbo.PR_Add_RecordSelect * from PR_Add_Record, PR_ReservationsWhere PR_Add_Record.resource_id = PR_Reservations.resource_idand (PR_Add_Record.start_time Between PR_Reservations.start_time and PR_Reservations.end_timeor PR_Add_Record.end_time BetweenPR_Reservations.start_time and PR_Reservations.end_time)Delete from dbo.PR_Add_Recordendelsebeginselect 'Sorry, that piece of equipment has already been reserved within the time frame requested.'end==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
DavisK
Starting Member
6 Posts |
Posted - 2005-11-23 : 10:45:22
|
| "if exists (Select * from PR_Add_Record, PR_ReservationsWhere PR_Add_Record.resource_id = PR_Reservations.resource_idand (PR_Add_Record.start_time Between PR_Reservations.start_time and PR_Reservations.end_timeor PR_Add_Record.end_time BetweenPR_Reservations.start_time and PR_Reservations.end_time))beginInsert into dbo.PR_Reservations "Won't this insert a duplicate time reservation into the PR_Reservations table before checking for conflicts?Thanks, |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-11-23 : 10:57:34
|
| Sorry - should be if not exists ( ...==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
DavisK
Starting Member
6 Posts |
Posted - 2005-11-23 : 11:15:32
|
| Perfect! Thanks very much! |
 |
|
|
|
|
|
|
|