Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 20th, 2008, 08:04 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help on Trigger

Hi All,

I have a problem with using Triggers. I have 2 tables which have the same columns but are situated on 2 different databases. I need to write triggers for Insert, Update on both databases, so that if I Insert a record in the Table in Database 1 it should be reflected in the Table in Database 2.Same vice-versa. There are Unique Keys on both sides on which Insert,update Triggers would run.

Kindy help me in how to go about it.

I am having problems in while Inserting as Inserting in Database Table is firig the Triggers for the table in the Other Database and resulting in error.
 
Old August 20th, 2008, 08:43 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I can think of three options that may help, it depends somewhat on what is meant by different databases, are they on the same server or different ones?
 
  • Look at setting nested triggers to 0 at some stage
  • Use replication instead of triggers
  • Examine the CONTEXT_INFO before executing trigger code, you can set the CONTEXT_INFO yourself to disable the second trigger


--

Joe (Microsoft MVP - XML)
 
Old August 20th, 2008, 02:36 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

As another really hacky way to do this (not recommended, but if for some reason Joe's suggestions can't work in your situation), you *could* simply add a dummy field to each table that your normal INSERT and UPDATE operations ignore (e.g., use a DEFAULT of NULL or some such). But in the trigger code, you purposely use a non-default value when you replicate the records into the other DB. And then the trigger code looks for a non-default value and does nothing when such is found.

Ugh. I can't believe I'm suggesting this. Please try Joe's suggestions first! (Especially see if you can't just use replication.)
 
Old August 21st, 2008, 02:07 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe & Old Pedant,

Thanks for your suggestions. Since the databases are on the same server I would first try setting nested triggers with Update and try setting them to 0. I am not too confident on replication so I would rather try this first.


Thanks again,
Arny





Similar Threads
Thread Thread Starter Forum Replies Last Post
Trigger Help monika.vasvani SQL Language 2 March 1st, 2007 06:59 AM
Trigger arshad mahmood C++ Programming 4 June 24th, 2004 07:10 AM
Trigger ! minhtri Pro VB Databases 2 June 23rd, 2004 02:27 AM
Trigger arshad mahmood SQL Language 2 May 12th, 2004 05:16 AM
Using instead of trigger dmr999 SQL Server 2000 1 November 29th, 2003 02:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.