Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 December 11th, 2003, 03:47 PM
Registered User
 
Join Date: Dec 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help With INSERT + UPDATE Trigger

Hello Everyone,

I am trying to create a trigger that will add or modify data in a table, every time a user adds or modifies data in another table. Any ideas would be greatly appreciate. Here is what I currently have:

-Two databses, call them DB1 and DB2.
-Two tables, each in one of the databases, call them DB1Tbl1 and DB2Tbl2.
-One column for each table, so DB1Tbl1Col1 and DB2Tbl2Col1.
-Every time a user adds or modifies data in DB1Tbl1Col1, this causes the same addition or modification in DB2Tbl2Col1.

I've written a trigger that does the INSERT part fine. It's the UPDATE part that's giving me problems. Here is the code as it now stands (without the UPDATE capability):

CREATE TRIGGER InsCol1Tbl1Tbl2Trig ON dbo.DB1Tbl1
FOR INSERT
AS
INSERT INTO dbo.DB2Tbl2 (DB2Tbl2Col1)
SELECT DB1Tbl1.DB1Tbl1Col1
FROM DB1Tbl1 INNER JOIN Inserted ON DB1Tbl1.DB1Tbl1Col1=Inserted.DB1Tbl1Col1;

Thanks.

Henry
 
Old December 11th, 2003, 06:26 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

you may consider not inserting into the table but instead update the table from table1 to table2

Create trigger "updatetables"
on db1.dbo.table1
for update, insert
not for replication
AS update db1.dbo.table1
set db1.dbo.table1.col1=db2.dbo.table2.col1
where db1.dbo.<table1.value> =db2.dbo.<table2.value>

if you are updating existing records use update if no go ahead with insert into. Is this for an audit table?

I hope this helps.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Trigger : after update insert into second table ik SQL Server 2000 4 January 30th, 2009 12:32 AM
trigger to insert current date on insert kev_79 SQL Server 2000 3 January 23rd, 2006 05:58 PM
insert,update trigger rolle SQL Server 2000 3 September 29th, 2005 08:41 PM
Insert Update Trigger mstuart60 SQL Server 2000 10 September 30th, 2003 06:54 AM
Update, insert and delete Trigger khautinh SQL Server 2000 2 September 17th, 2003 11:45 AM





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