473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Nested Triggers


I'm evidently not understanding nested triggers and I'm looking for
some help.

I've an Invoice table (see below) that I want to enforce two actions
via after triggers.

The first trigger maintains a set of audit columns in the table
indicating the date on which the row was inserted, the date of the
last change, and the user who made the last change (see below)

The second trigger maintains the invoice number across revisions. A
specific invoice for a contract may have multiple rows as revisions
are made during processing. So if a change is made to an invoice
number in one row, the trigger makes sure that all the associated
revision rows are updated with the new invoice number. (see below)

If I implement these as two triggers, I hit the 32 level limit and I
don't understand why.

Regardless of the order (and I haven't set the first trigger so the
order is undefined) I would expect that my second trigger would
execute only once; that after one execution, there are no rows with
the old invoice number and its execution would NOT initiate the first
trigger.

So I expect to get three or four levels of trigger execution depending
on the order.

But with two triggers, it consistently reports exceeding the 32 level
limit restriction.

I've combined the processing into one trigger to get around this
problem (with some small modifications to process the audit columns),
but I really would like to understand what's going on.

Any help would be appreciated.
*******
******* Abbreviated table def
*******
CREATE TABLE [tblInvoices] (
[Inv_id] [int] IDENTITY (1, 1) NOT NULL ,
[Contract_Info_ID] [int] NOT NULL ,
[Invoice_No] [varchar] (50) NOT NULL ,
[Invoice_Revision] [int] NOT NULL ,
[inv_audit_entry_date] [datetime] NULL ,
[inv_audit_change_date] [datetime] NULL ,
[inv_audit_change_user] [varchar] (255) NULL ,
[inv_timestamp] [timestamp] NULL ,
CONSTRAINT [PK_tblInvoices] PRIMARY KEY CLUSTERED
(
[Inv_id]
) ON [PRIMARY] ,
CONSTRAINT [FK_tblInvoices_tblContractInformation] FOREIGN KEY
(
[Contract_Info_ID]
) REFERENCES [tblContractInformation] (
[Contract_Info_ID]
)
) ON [PRIMARY]
*******
*******
************************************************** ******
********
******** First Trigger - Audit column trigger
********
declare @wrkDate datetime
set @wrkDate = GETDATE()

declare @wrkUser varchar(255)
set @wrkUser = SUSER_SNAME()

update tblInvoices
set inv_audit_entry_date = coalesce(t1.entryDate, @wrkDate),
inv_audit_change_date = @wrkDate,
inv_audit_change_user = @wrkUser
from (select i1.inv_id as entryID, d1.inv_audit_entry_date as
entryDate
from inserted as i1
left join deleted as d1 on d1.inv_id = i1.inv_id) as t1
where inv_id = t1.entryID
*******
*******
************************************************** ******
*******
******* Second Trigger - Invoice Number
*******
update tblInvoices
set invoice_no = t1.newInvoiceNo
from (select i1.contract_info_id as ContractInfoID, i1.invoice_no
as newInvoiceNo, d1.invoice_no as oldInvoiceNo
from inserted as i1
inner join deleted as d1 on d1.inv_id = i1.inv_id
where d1.invoice_no <> i1.invoice_no) as t1
where contract_info_id = t1.ContractInfoID
and invoice_no = t1.oldInvoiceNo
*******
*******
Dec 3 '05 #1
1 2344
Mark Flippin (me******@comcast.net) writes:
Regardless of the order (and I haven't set the first trigger so the
order is undefined) I would expect that my second trigger would
execute only once; that after one execution, there are no rows with
the old invoice number and its execution would NOT initiate the first
trigger.


As if the trigger would care about that. :-)

Add this to both triggers:

IF @@rowcount = 0
RETURN

Triggers fires even if no rows are affected.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 3 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark Flippin | last post by:
I'm just starting to use triggers in my databases and find the support in Enterpise Manager lacking. Using Enterprise Manager and Query Analyzer you can maintain the triggers, but it's...
1
by: tim.pascoe | last post by:
I'm trying to generate scrips for a database, and everything so far has worked fine, except for the triggers. When I try and script existing triggers, all I get is a blank file - no SQL script. I...
9
by: John Sidney-Woollett | last post by:
Is it possible to use the dblink and dblink_exec features from inside pl/pgsql functions to mimic the behaviour of nested transactions by calling another function or executing some SQL via the...
0
by: lior3790 | last post by:
Hello to all the smart people, I'm trying to create a composite control which include nested controls from the great Ajax Dev. I overcame a lot of issues and can be helpful in some but my...
0
debasisdas
by: debasisdas | last post by:
trigger sample code Ex#10 ======================= INSTEAD OF TRIGGER ---------------------------------------- create or replace trigger mytrig instead of delete or insert or update on eview...
0
debasisdas
by: debasisdas | last post by:
This thread contains some useful tips/sample codes regarding TRIGGERS in oracle, that the forum members may find useful. TRIGGERS: =============== Database trigger is a PL/SQL block that is...
6
by: RobertTheProgrammer | last post by:
Hi folks, Here's a weird problem... I have a nested GridView setup (i.e. a GridView within a GridView), and within the nested GridView I have a DropDownList item which has the...
4
by: --CELKO-- | last post by:
I need to convert a bunch of DB2 triggers to Oracle. Is there any kind of tools for this?
1
by: SimonZ | last post by:
I have nested update panels. When I click on button, which is located in parent update panel, I would like that only child update panel is refreshed. Now the both panels are refreshed or none...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.