I am studying the trigger and I got a question.
I am creating a trigger to change the time complex.
In the data table weeklymeeting
|section_id| meeting type| days| timestart|timeend|class_id|
| 13 | Lecture | 5 | 01:00:00 |02:00:00|12 |
timestart and timeend are of datatype TIME
I am creating the trigger that checking the time complex
for example
if user uses query like
INSERT INTO DBO.WeeklyMeetings VALUES (35, 1,1 ,'11:40:00','11:42:00', 42)
it should insert the data
however
INSERT INTO CSE132B.DBO.WeeklyMeetings VALUES (35, 1, 1, '12:30:00','01:30:00', 42)
then it must return error message because it is time complex
Therefore I created a trigger to check the time complex..but such as
ALTER TRIGGER [dbo].[check_section_time_complex] ON [dbo].[WeeklyMeetings]
FOR INSERT AS
IF EXISTS(select * from inserted as i INNER JOIN dbo.WeeklyMeetings as m ON (i.timestart BETWEEN m.timestart and m.timeend)
OR( i.timeend BETWEEN m.timestart and m.timeend ))
begin
RAISERROR ('Can not update data because section time is complexing with other section!!', 16,1);
rollback tran
end
however, it is not really working…i am not really sure that reason….
does anyone knows problems in this trigger ?
thanks
If by “it’s not really working” you mean that it’s allowing conflicting data to be inserted and then raising an error, this is because you’ve specified a
FOR INSERTtrigger which means that it’s happening after the data is inserted.If you want to prevent the data being inserted you’ll need to use an
INSTEAD OFtrigger which happens before insert and within that do the insert yourself if it is valid.See http://msdn.microsoft.com/en-us/library/ms189799.aspx for the trigger definition.
If the error is something else please describe it more fully.