|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

September 26th, 2005, 08:39 AM
|
|
Registered User
|
|
Join Date: Sep 2005
Location: , , Finland.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

September 26th, 2005, 02:14 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,328
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|

September 27th, 2005, 05:58 AM
|
|
Registered User
|
|
Join Date: Sep 2005
Location: , , Finland.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

September 29th, 2005, 09:41 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Location: Orange County, CA, USA.
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |