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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 help with trigger

Author  Topic 

sqlneofito
Starting Member

16 Posts

Posted - 2010-04-05 : 18:49:46
I need to run this INSERT trigger on an DErrorLog table that counts the number of times the ErrorNumber that is currently being inserted already exists in the table.. with an output to the screen that says "The error: (errornumber) has ocurred X times before"
It should be easy but I just can't see the light.
Thank you for any suggestions.
This is what I have so far


create trigger InsTriggErrLog on DErrorLog
after insert
as
Declare @t int
Inserted.ErrorNumber

IF exists (select I.ErrorNumber from Inserted as I inner join DErrorLog as D
on I.ErrorNumber = D.ErrorNumber)

set @t = @@ROWCOUNT

---print 'This error:'; @e 'error has ocurred'; @t-1, 'number of times in the past'
print 'number of times in the past' + convert(varchar(25), @t)
--print 'number of times in the past' + convert(varchar(25), @e)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-05 : 19:10:40
You can't use @@ROWCOUNT after IF EXISTS since it isn't returning rows.

create trigger InsTriggErrLog on DErrorLog
after insert
as

IF (select COUNT(*) from Inserted as I join DErrorLog as D on I.ErrorNumber = D.ErrorNumber) <> 0
print 'number of times in the past' + convert(varchar(25), @t)
GO



Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sqlneofito
Starting Member

16 Posts

Posted - 2010-04-05 : 19:26:58
Hello Tara, thank you for your help, but, where do I declare the value of 'select count' to print the message

Thank you again!!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-05 : 20:01:57
Sorry, I pasted in the wrong one.

create trigger InsTriggErrLog on DErrorLog
after insert
as

DECLARE @t int

select @t = COUNT(*)
from Inserted as I
join DErrorLog as D
on I.ErrorNumber = D.ErrorNumber

IF @t <> 0
print 'number of times in the past' + convert(varchar(25), @t)
GO

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sqlneofito
Starting Member

16 Posts

Posted - 2010-04-05 : 20:23:54
Tara you save my day again!! ..thank you very much!!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-05 : 22:27:47
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -