473,659 Members | 3,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_I D] [int] NOT NULL ,
[Invoice_No] [varchar] (50) NOT NULL ,
[Invoice_Revisio n] [int] NOT NULL ,
[inv_audit_entry _date] [datetime] NULL ,
[inv_audit_chang e_date] [datetime] NULL ,
[inv_audit_chang e_user] [varchar] (255) NULL ,
[inv_timestamp] [timestamp] NULL ,
CONSTRAINT [PK_tblInvoices] PRIMARY KEY CLUSTERED
(
[Inv_id]
) ON [PRIMARY] ,
CONSTRAINT [FK_tblInvoices_ tblContractInfo rmation] FOREIGN KEY
(
[Contract_Info_I D]
) REFERENCES [tblContractInfo rmation] (
[Contract_Info_I D]
)
) 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.ent ryDate, @wrkDate),
inv_audit_chang e_date = @wrkDate,
inv_audit_chang e_user = @wrkUser
from (select i1.inv_id as entryID, d1.inv_audit_en try_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_inf o_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_i d = t1.ContractInfo ID
and invoice_no = t1.oldInvoiceNo
*******
*******
Dec 3 '05 #1
1 2355
Mark Flippin (me******@comca st.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****@sommarsk og.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
2530
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 cumbersome. Are there better tools for creating and managing triggers? Mark Flippin
1
1825
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 tried single files for each object, all in one file, triggers only, the entire database. I can't figure it out, but I know the triggers are there. Any suggestions, or am I missing something undocumented in SQL-Server 2000?
9
7599
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 dblink (into the same database)? Does the SQL statement executed within the dblink sit in its own isolated transaction/session? And would an error thrown in the dblink statement abort the enclosing session that initiated the call? What is the...
0
2184
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 latest problem doesn't seem to disappeared. I can't trigger the ITemplate that i declared (maybe improperly?) to show first a Gif on the progress and afterwords the calculated information from the database.
0
8317
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 declare a number(2); begin
0
4489
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 executed on an event in the database. The event is related to a particular data manipulation of a table such as inserting, deleting or updating a row of a table. Triggers may be used : 1.To implement complex business rule, which cannot be...
6
5749
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 OnSelectedIndexChanged event set on it. This triggers just fine, but within the codebehind of the OnSelectedIndexChanged event, I need to scan through all the entries in the nested GridView (to see if the user changed a value to an already existing value in the...
4
5734
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
5023
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 if I set ChildrenAsTriggers="false" on parent update panel. How can I do that? thanks, Simon
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.