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, 2003, 03:36 AM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Insert Update Trigger

I need to put a trigger on a table to ensure that the date it was last modified is automatically put in the column Last_Mod and also the name of the user who modified the record into the column updated_by. Can someone please help me with the syntax.

Many thanks

Mike
 
Old September 26th, 2003, 04:11 AM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Further to this. When I do put a trigger on this table then it stops me from updating and inserting new records. Its very strange. When I take the Trigger off I can now insert and update records. Is there a lock happening somewhere that is not llowing me to insert or update.

Mike
 
Old September 26th, 2003, 08:16 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by mstuart60
 ... When I do put a trigger on this table then it stops me from updating and inserting new records. ...

Post the code for trigger you tried.

There is nothing inherent in a trigger which "stops" you from doing an insert, unless, of course, you have defined an INSTEAD OF trigger which executes, er, instead of, the UPDATE query which fires it.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 26th, 2003, 09:10 AM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff have used the following code.
CREATE TRIGGER lastmod
ON tbl_session_booking
after INSERT, update
AS
insert into tbl_session_booking (last_mod, Updated_by)
Values (getdate(), suser_sname())

select last_mod, updated_by from Inserted

But when I input a record set I get a warning coming up that I cant insert nulls into one of the fields. Is it my select Statement that is wrong for the trigger.

Thanks
Mike
 
Old September 26th, 2003, 09:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Why are you INSERTing another row inside your trigger? In your original post, you indicated you wanted to update rows to add a last modified date and User name. The error occurs because your INSERT statement isn't complete - an INSERT inserts an entire new row and you must have some non-null columns defined in your table which your INSERT statement in the trigger isn't supplying a value for.

Your trigger should UPDATE the rows in the base table as identified by the 'inserted' table, as, for example:
Code:
UPDATE tbl_session_booking
    SET last_mod=current_timestamp,
        Updated_by=suser_sname()
    FROM tbl_session_booking INNER JOIN inserted
        ON tbl_session_booking.<primarykeycolumn>=inserted.<primarykeycolumn>;
where <primarykeycolumn> is the primary key of your table.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 26th, 2003, 09:58 AM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff.

I have the following columns in that table

booking_id, course_number, student_id, Session_date

Mike
 
Old September 26th, 2003, 10:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Now you've thoroughly confused me.

Where are the 'Last_Mod' and 'Updated_by' columns mentioned in your original post?

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 26th, 2003, 10:51 AM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff,

the columns are in the table i should have included them in the last reply.

booking_id, course_number, student_id, session-date, last_mod, updated_by

sorry for the confusion.

Mike
 
Old September 26th, 2003, 11:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

OK. So, the trigger should do an UPDATE as I described above, and not an INSERT as you attempted.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 30th, 2003, 03:52 AM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff,

I must be doing something far wrong. The primary key for the table is booking_id. Can you be so kind to write the trigger out in full that I should put in.
I keep getting an error that it does not recognise column tbl_session_booking.

Many Thanks

Mike





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