Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update trigger

Author  Topic 

cdene
Starting Member

1 Post

Posted - 2005-09-21 : 16:43:41
I am new to sql triggers and tried to create one that updates a date field but what it does is add a new row with the update field.
can someone please help me fix this?

CREATE TRIGGER Tg_Update_close ON [dbo].[tblTicket]
for
UPDATE
AS
insert into tblticket (datecomplete)
select getdate()

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-09-21 : 16:48:32
You need to use the inserted trigger table to determine what rows were updated. You join to that to update those specific rows. Check out deleted and inserted trigger tables in SQL Server Books Online if you aren't familiar with them.

CREATE TRIGGER utrg_tblTicket
ON [dbo].[tblTicket]
FOR UPDATE
AS

UPDATE t
SET datecomplete = GETDATE()
FROM tblticket t
INNER JOIN inserted i
ON t.YourPKColumn = i.YourPKColumn

Tara
Go to Top of Page
   

- Advertisement -