Author |
Topic |
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-04 : 10:56:59
|
Hi I have a trigger which seems to work sometimes but not others. I know there must be a pattern to this but can't see it.If I update either my faultstart or faultend fields the duration field should update, but it doesn't seem to always work.=================================================USE [DB1]GO/****** Object: Trigger [dbo].[DURATION_UPDATE] Script Date: 08/04/2010 15:47:55 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: <ICW>-- Create date: <04/08/10>-- Description: <Trigger to update the Duration field in calllog with the duration of a fault>-- =============================================ALTER TRIGGER [dbo].[DURATION_UPDATE] ON [dbo].[Detail]FOR INSERT, UPDATEASDeclare @CalliD Varchar(8) Declare @StartDate Datetime Declare @EndDate DatetimeSelect @callid=Callid FROM detail WHEREisdate(faultstart)=1 and isdate(faultend)=1IF @Callid IS NOT NULLBeginSelect @StartDate = cast ([faultstart]+' '+[faultstarttime] as datetime) from detail where callid = @CallID Select @EndDate = cast ([faultend]+' '+[faultendtime] as datetime) from detail where callid = @CallIDUPDATE CallLogSet RepDuration = datediff(ss, @StartDate , IsNull(@EndDate,@StartDate))/60 FROM DETAILWhere calllog.CallID = @CallIDEND |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-04 : 11:04:25
|
seem to be working on insert but not update |
 |
|
denis_the_thief
Aged Yak Warrior
596 Posts |
Posted - 2010-08-04 : 11:15:12
|
quote: Originally posted by icw seem to be working on insert but not update
How about, as a test, make a TestTable and inside the trigger, log @StartDate, @EndDate, @CallID, datediff(ss, @StartDate , IsNull(@EndDate,@StartDate))/60 along with getdate() to the TestTable. |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-04 : 11:29:17
|
I've noticed it only updates the most recent row.What could be casuing this?Any ideas |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-04 : 12:03:54
|
just figured it out.I had putSelect @callid=Callid FROM detail but i should have putSelect @callid=Callid FROM inserted |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-08-04 : 13:00:58
|
You are assuming that the trigger is only called with one row. The trigger will be called once for all rows in the Insert / Update, therefore you need to make the trigger able to handle multiple rows in Inserted / Deleted. |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-04 : 13:42:50
|
Sorry I don't understandcould you lay that out in a way a stupid person (like me) can understandcheers |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-08-04 : 15:36:27
|
I insert multiple rows:INSERT INTO [dbo].[Detail] (Col1, Col2, ...)SELECT Col1, Col2, ...FROM SomeOtherTableor I update multiple rows:UPDATE [dbo].[Detail]SET Col1 = Col1 + 10WHERE Col2 = 'XXX'the trigger will only be called once, and INSERTED table will contain all the rows that were inserted / updated.Your trigger code is assuming that there will only be ONE row in INSERTEDi.e. :Select @callid=Callid FROM insertedUPDATE CallLogSet RepDuration = ...Where calllog.CallID = @CallID |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-04 : 16:27:10
|
Thanks a lot guys. In this case i think it's fine because the application is updating only one row at at a time and only ever updates one row at a timeOr did i not get it? |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-08-04 : 16:30:53
|
No, you get it, but sooner or later someone / somehow multiple records will be updated.At the least put something like:IF (SELECT COUNT(*) FROM Inserted) <> 1BEGIN RAISERROR(... ROLLBACK ... RETURN -1 ... ...END so that it doesn't silently only process one random record when someone, someday, does process multiple records. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-04 : 17:12:28
|
OK - I know when I'm being told off!Problem is - I don't know how to do the "raiserror" thing. In fact I didn't even know about itSee below I've tried it but obviously got the syntax wrong.Could you point out what i got wrong?Thanks a lot================================USE [DB1]GO/****** Object: Trigger [dbo].[DURATION_UPDATE] Script Date: 08/04/2010 18:46:01 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: <ICW>-- Create date: <04/08/10>-- Description: <Trigger to update the Duration field in calllog with the duration of a fault>-- =============================================ALTER TRIGGER [dbo].[DURATION_UPDATE] ON [dbo].[Detail]FOR INSERT, UPDATEASIF (SELECT COUNT(*) FROM Inserted) <> 1BEGIN RAISERROR( 'Cannot update more than one row') ROLLBACK transaction RETURNELSEDeclare @CalliD Varchar(8)Declare @SwernCode Varchar (30)Declare @FaultStart Varchar (10)Declare @FaultEnd Varchar (10)Declare @faultStartTime varchar (8)Declare @FaultEndTime varchar (8)Declare @DetailFS datetimeDeclare @DetailFE datetimeSelect @callid=Callid FROM inserted WHEREisdate(faultstart)=1 and isdate(faultend)=1IF @Callid IS NOT NULLBeginSelect @DetailFS = Cast([Faultstart]+' '+[Faultstarttime] as datetime)from detailWhere callid = @CallidSelect @DetailFE = Cast([Faultend]+' '+[Faultendtime] as datetime)from detailWhere callid = @CallidSelect @Swerncode = SWERNAccServSubCallTypefrom detailWhere callid = @CallidSelect @FaultStart = Faultstartfrom detail where callid = @CallID Select @FaultEndTime = FaultEndTimefrom detail where callid = @CallIDSelect @FaultEnd = FaultEndfrom detail where callid = @CallIDSelect @FaultStartTime = FaultstartTimefrom detail where callid = @CallIDUPDATE CallLogset calllog.SwernCode = @Swerncode,calllog.faultstart = @FaultStart,calllog.FaultEnd = @FaultEnd,Calllog.FaultstartTime = @faultstarttime,Calllog.faultendtime = @faultendtime,RepDuration = datediff(ss,@DetailFS ,@DetailFE)/60Where calllog.CallID = @CallIDEND |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-08-04 : 17:24:59
|
ThanksI have referenced BOL and give the error a 16,1 and it parses correctly, howvere when I try to run the statemntIt now gives me the following error Msg 208, Level 16, State 1, Line 11Invalid object name 'inserted'.Whereas before it worked fine and recognised the inserted pseudo table no problem. |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-08-05 : 03:26:34
|
"Whereas before it worked fine and recognised the inserted pseudo table no problem."Sounds like what you are looking at, thinking its the problem, isn't.Post the syntax and we'll have a look.Get your fireproof underware on first !!!! |
 |
|
|