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 September 26th, 2005, 07:39 AM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default insert,update trigger

Hey!

I have a problem using "for insert, update" -style trigger. It works nicely with table updates but inserts do not seem to work. Here is a sample of the trigger:


CREATE TRIGGER customer_trigger ON dbo.CUSTOMER FOR INSERT, UPDATE AS BEGIN DECLARE @col_name varchar(30),@old_value varchar(80),@new_value varchar(80) SELECT @col_name = 'NAME',@old_value = del.NAME,@new_value = ins.NAME FROM deleted del, inserted ins IF @new_value<>@old_value BEGIN INSERT INTO HISTORY_TABLE(FIELD,OLDVALUE,NEWVALUE,TIME) SELECT @col_name,@old_value,@new_value,getDate() FROM deleted del, inserted ins END END


I haven't worked with triggers much so I appreciate all the help I can get.
 
Old September 26th, 2005, 01:14 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

It is unclear what you are trying to do. I do see a proble in that you are selecting from 2 tables but there is no JOIN condition.

Ex:
 FROM deleted del, inserted ins

These tables are not joined in any way.

Please explain in more detail what you are trying to accomplish.

Jim

 
Old September 27th, 2005, 04:58 AM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm trying to create a trigger which would insert a row to the table HISTORY_TABLE every time a specified row in the table CUSTOMER is updated or if a new row is inserted. A New row is inserted to HISTORY_TABLE for every updated field in CUSTOMER table, so if I insert a new row to CUSTOMER, it would add a new row to HISTORY_TABLE for every field that had a value added to it.

Again, it works when I update a row in CUSTOMER table, but it doesn't
work when I insert a new row to the table.

I hope this makes more sense.


Thanks,

Janne

 
Old September 29th, 2005, 08:41 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think the problem is your comparing the new value to the old. On an insert there is no old value to compare to. Thus nothing to compare to or null. Thus your logic works on update but not on insert. I am also rusty on this. To trouble shoot I suggest you write some test code and insert 100% of the new values into the test table (don't do a compare, thus no "if"). First write just the new or inserted value, then try to also write the "old" value. Then if you do one insert and one update you should see the differenc in how sql treats things in your test table.






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
Help With INSERT + UPDATE Trigger HenryE SQL Server 2000 1 December 11th, 2003 06:26 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.