Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create trigger 1

Status
Not open for further replies.

beechill4

Technical User
May 10, 2005
207
0
0
IE
Hi,

I have a table called healthchecks
it has 2 fields, one call sentdate, the other called nextauditdate.

I want to create a trigger which updates the nextauditdate to sentdate plus 1 month whenever a new record is inserted.

help much appreciated.

Joe
 
Is there any link between these two tables?

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
CREATE TRIGGER [TriggerName] ON [DatabaseName].[healthchecks]
FOR INSERT
AS

IF EXISTS (SELECT * FROM inserted)

BEGIN
update [DatabaseName].[healthchecks]
set nextauditdate=dateadd(month,1,sentdate)
where unique_id in (select unique_id from inserted)
END

make sure sentdate is not null
and if you have some unique_id on the the table.

Thanks



SQL Help Blog
 
hi,

i'm a bit confused with this line:
where unique_id in (select unique_id from inserted)

what does that mean?

Thanks
 
I am confused also along with beechill4. Why is it needed if you are dealing with one row at a time?

Jim
 
2 tables... stupid me.

EXISTS() is not necessary - either INSERT happens (and trigger fires) or not.

Multirow inserts are not a problem, as long as there is unique/primary key to catch on (unique_id).

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
if i just want to update the row i have inserted or updated, how do i handle that?

 
I don't think you can. I believe you have to grab the value from the Inserted row and update the table based on some unique ID from the Inserted row. That is what nomadicalloy and vongrunt are trying to point out
 
ok!

my problem is that when i create the trigger with the line
where unique_id in (select unique_id from inserted)
i get an error....

Server: Msg 207, Level 16, State 3, Procedure joe, Line 8
Invalid column name 'unique_id'.
Server: Msg 207, Level 16, State 1, Procedure joe, Line 8
Invalid column name 'unique_id'.
 
Yes. He is not proposing to use that exact code. He is trying to tell you to substitue your unique column ID in the code.
 
Hi,

apologies if this sounds dumb!

each record in this table is uniquely identified by 2 columns:
servername
and
sentdate.

how would this work?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top