Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 26th, 2005, 07:39 AM
Registered User
 
Join Date: Sep 2005
Location: , , Finland.
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.
Reply With Quote
  #2 (permalink)  
Old September 26th, 2005, 01:14 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
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

Reply With Quote
  #3 (permalink)  
Old September 27th, 2005, 04:58 AM
Registered User
 
Join Date: Sep 2005
Location: , , Finland.
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

Reply With Quote
  #4 (permalink)  
Old September 29th, 2005, 08:41 PM
Friend of Wrox
 
Join Date: Aug 2004
Location: Orange County, CA, USA.
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:35 AM.


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