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 2000 Forums
 SQL Server Development (2000)
 Running only one instance of a trigger

Author  Topic 

sajsal
Starting Member

4 Posts

Posted - 2004-02-07 : 05:20:11
I have applied a trigger on inserts of a table. The triger might execute for 10 or even more seconds depending upon the data in the table. The trigger also affect other tables as well.

When a record is inserted into the table, the triger fires and starts executing. Now suppose that as soon as the trigger fires, another record is inserted. Logically, another instance of the trigger must fire and start executing.

Now what I want to know is a method that stops other instance to start after the first finishes. In simple words only one instance of the trigger must execute at one time.

A code example will be appreciated alot.

regards
Sajid

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-02-07 : 09:27:40
What are you trying to accomplish in your trigger?
Go to Top of Page

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2004-02-07 : 09:59:21
How about to create a tiny one-row table:

create table trigger_status (st bit)
insert into trigger_status select 0

Then, in the trigger body, check it:

AS
if (select top 1 st from trigger_status with(nolock))=0
begin
update trigger_status set st=1

{here "main" part of the trigger}

update trigger_status set st=0
end
Go to Top of Page

sajsal
Starting Member

4 Posts

Posted - 2004-02-09 : 05:04:31
Thank you Stoad for your reply.

Idea is great, BUTT

when the second instance will execute, it will perform nothing. I want the trigger to perform all the processing for each insert. Any ideas

Thanks

Sajid
Go to Top of Page

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2004-02-09 : 07:03:59
OK. How about to check it in a while loop?

AS
while (select top 1 st from trigger_status with(nolock)) <> 0
waitfor delay '00:00:00.05'
begin
update trigger_status set st=1
Go to Top of Page
   

- Advertisement -