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 |
|
bxw689
Starting Member
4 Posts |
Posted - 2008-12-13 : 22:36:18
|
| I am trying to use the trigger below to prevent the double of appointments in a physician database. When I install the trigger, it rolls back all appointments, not just those that meet the give criteria. I don't see where the problem is. Can anyone help me figute this out. Thanks!CREATE TRIGGER trg_physician_double_bookingon appointmentsfor insertasbegindeclare @physician_id int, @appointment_date date, @start_time time, @end_time timeselect @physician_id = (select physician_id from inserted) select @appointment_date = (select appointment_date from inserted) select @start_time = (select start_time time from inserted)select @end_time = (select end_time time from inserted)if exists (select 1 from appointments where physician_id = @physician_id and appointment_date = @appointment_date and end_time > @start_time) begin rollback transaction print 'Duplicate Appointment' end end |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-13 : 23:36:46
|
it should be an instead of triggerCREATE TRIGGER trg_physician_double_bookingon appointmentsinstead of insertasbeginif not exists (select 1 from appointments ainner join inserted ion i.physician_id = a.physician_id and i.appointment_date = a.appointment_date and i.end_time > i.start_time)begininsert into appointmentsSELECT fields...FROM insertedendelsebeginrollback transactionprint 'Duplicate Appointment'endend |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-12-14 : 02:17:48
|
| You need a unique constraint not a trigger. |
 |
|
|
|
|
|