SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 After Insert Trigger Problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Feras82
Starting Member

Kuwait
3 Posts

Posted - 05/10/2012 :  03:51:25  Show Profile  Reply with Quote
Hello,
I have the following scenario
I wrote a very simple test trigger


Here is the code:

ALTER TRIGGER [dbo].[Test2]
ON [dbo].[Messages]
AFTER insert
AS
BEGIN
declare @id int
select @id = id from inserted
update smsserver.dbo.messages set smsserver.dbo.messages.body = left(smsserver.dbo.messages.body, len(smsserver.dbo.messages.body)-3)
from smsserver.dbo.messages
where smsserver.dbo.messages.id = @id
END


The trigger simply update a value of some column after inserting, but actually the trigger is blocking inserting until I disable it, then the id filed (identity) will take a very big unexpected value depending on the period between inserting the record and disabling the trigger, which gives the sense that something is going into infinite loop until disabling.


Regards,

Syrian4ever

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 05/10/2012 :  07:44:35  Show Profile  Reply with Quote
I don't see anything in your trigger that would cause it to blocking insert. Do you have any update triggers on the table? If you do, is recursive triggers turned on your database? By default it is off, but you can check using the following query:
SELECT is_recursive_triggers_on FROM sys.databases WHERE NAME = 'thedatabasename'

While the following may not help you in solving the problem you should change your code to the following:
ALTER TRIGGER [dbo].[Test2]
ON [dbo].[Messages] 
AFTER insert
AS 
BEGIN
UPDATE smsserver.dbo.messages
SET    smsserver.dbo.messages.body = LEFT(
           smsserver.dbo.messages.body,
           LEN(smsserver.dbo.messages.body) -3
       )
FROM   smsserver.dbo.messages
	INNER JOIN INSERTED ON INSERTED.id = smsserver.dbo.messages
END
It is logically the same if only one row is inserted in an insert statement, but will allow the correct behavior if multiple rows are inserted.
Go to Top of Page

Feras82
Starting Member

Kuwait
3 Posts

Posted - 05/13/2012 :  02:01:13  Show Profile  Reply with Quote
Thank you Sunitabeck
The value of SELECT is_recursive_triggers_on FROM sys.databases WHERE NAME = 'thedatabasename' is 0

and I had used your code, but unfortunetly I still have the same issue.

Syrian4ever
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 05/13/2012 :  15:28:39  Show Profile  Reply with Quote
why do you need to do this via trigger?
also does your table have a clustered index?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000