Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with possible infinite looping triggers

Posted on 2008-06-09
3
Medium Priority
?
259 Views
Last Modified: 2008-06-16
Hi,

I have 2 store databases (retail & wholesale), each database contains the same tables and structure but most with different data. There are several tables that will contain the same information such as products, productOptions, categories, etc.

I have two seperate web apps for updating each database. Each page contains several forms for updating the various tables and submits to an execute page that carries out the updates.

I have created (with help from expert SIMONET) a trigger that will update/insert/delete the wholesale database after the retail one has been updated thru the web app.

Everything works great until I try to create the same type of triggers for the wholesale database (to perform the updates on the retail database).

I think I have created a infinite loop of triggers. I am not really sure because the error is:

 [DBNETLIB][ConnectionRead (recv()).]General network error. Check your network documentation.

But it seems logical that the trigger are acting on each other.

Can anyone help to solve this situation? Here is the trigger code:
USE [jcpascu_retailDEV]
GO
 
create trigger [jcpascu_admin].[tr_products]
   on [jcpascu_admin].[products]
   after Insert,Update,Delete
as
 
begin
  
   declare @InsertedCount int
   declare @DeletedCount int
   set @InsertedCount = (select count(*) from inserted)
   set @DeletedCount = (select count(*) from deleted)
 
   if @InsertedCount > 0 begin
      -- first do updated rows
      if @DeletedCount > 0 begin
         update [jcpascu_wholesaleDEV].[jcpascu_admin].[products]
           set price = A.price,
           noShipCharge = A.noShipCharge,
           fileName = A.fileName,
           homepage = A.homepage,
           smallImageUrl = A.smallImageUrl,
           listPrice = A.listPrice,
           sku = A.sku,
           relatedKeys = A.relatedKeys,
           details = A.details,
           descriptionLong = A.descriptionLong,
           description = A.description,
           imageUrl = A.imageUrl,
           reviewAllow = A.reviewAllow,
           stock = A.stock,
           weight = A.weight,
           reviewAutoActive = A.reviewAutoActive,
           refurbished = A.refurbished,
           msrp = A.msrp,
           handlingfee = A.handlingfee,
           AltImageURL = A.AltImageURL,
           Freight = A.Freight,
           active = A.active,
           supplier = A.supplier,
           sortOrder = A.sortOrder,
           taxExempt = A.taxExempt,
           UPC = A.UPC,
           minqty = A.minqty,
           idBrand = A.idBrand,
           hotDeal = A.hotDeal,
           idManufacturer = A.idManufacturer,
           mpn = A.mpn,
           wholesaleprice = A.wholesaleprice,
           map = A.map,
           estimatedship = A.estimatedship,
           XLrgImageURL = A.XLrgImageURL
         FROM Inserted A
         WHERE A.idProduct in (select [idProduct] from deleted) and ([jcpascu_wholesaleDEV].[jcpascu_admin].[products].idProduct = A.idProduct)
      end
      else
      begin
          -- now do inserted rows
          SET IDENTITY_INSERT [jcpascu_wholesaleDEV].[jcpascu_admin].[products] ON
          INSERT INTO [jcpascu_wholesaleDEV].[jcpascu_admin].[products]
           ([idProduct],price,noShipCharge,fileName,homepage,
           smallImageUrl,listPrice,sku,relatedKeys,
           details,descriptionLong,description,imageUrl,
           reviewAllow,stock,weight,reviewAutoActive,
           refurbished,msrp,handlingfee,AltImageURL,Freight,
           active,supplier,sortOrder,taxExempt,UPC,minqty,
           idBrand,hotDeal,idManufacturer,mpn,
           wholesaleprice,map,estimatedship,XLrgImageURL)
           select [idProduct],price,noShipCharge,fileName,homepage,
           smallImageUrl,listPrice,sku,relatedKeys,
           details,descriptionLong,description,imageUrl,
           reviewAllow,stock,weight,reviewAutoActive,
           refurbished,msrp,handlingfee,AltImageURL,Freight,
           active,supplier,sortOrder,taxExempt,UPC,minqty,
           idBrand,hotDeal,idManufacturer,mpn,
           wholesaleprice,map,estimatedship,XLrgImageURL
          FROM Inserted
          SET IDENTITY_INSERT [jcpascu_wholesaleDEV].[jcpascu_admin].[products] OFF
      end
 end
    else
          -- now do deleted rows
          if @DeletedCount > 0  begin
            DELETE FROM [jcpascu_wholesaleDEV].[jcpascu_admin].[products] 
            WHERE idProduct IN (SELECT idProduct FROM deleted)
          end
end

Open in new window

0
Comment
Question by:ChristinaPupo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Expert Comment

by:sqlconsumer
ID: 21749352
Do you have the code for the trigger on the Wholesale database? or is it exactly the same? appart from the database scope.

What you have describe is indirect recursion also referred to as nested triggers.

By setting the NESTED TRIGGERS option to 0 at the server level, you can disable all forms of nested trigger.
0
 

Author Comment

by:ChristinaPupo
ID: 21757837
The code for the wholesale triggers are the same except that they update the retail database instead. I don't think I have the option of changing the nested triggers option to 0 because I am on a shared SQL server and do not have access to most of the settings. Can you elaborate on how to do this so I can be sure? Isn't there any way of changing the trigger code to not act on each other? I have tried some variations of code using IF THEN statements and/or IF NOT UPDATE but these are the first triggers I've ever worked with and can't seem to get anything to work

You help is greatly appreciated..
0
 
LVL 1

Accepted Solution

by:
sqlconsumer earned 2000 total points
ID: 21767450
sp_configure 'nested trigers', 0;
GO

Will turn off nested triggers.

The alternative is to create a third table that has a trigger. When records are added to this table you can decide whether to apply the new record to the wholesale and retail tables.

Hope this helps
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question