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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Trigger works only for one time!

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-08-13 : 13:09:29
Mazdak writes "I have two date column(date1,date2) and one varchar column(datediff1) and one column for identity(Sou_ID). I write this trigger so when one row inserted into table it gets two dates and compute the difference and put it in varchar column. At this point its ok but when I close the table and open it again to insert new row only my last row has DATEDIFF and others got NULL. :(



ALTER Trigger tr1 on Source for insert as
update source set datediff1=
(select CONVERT(varchar(50),DATEDIFF(month,
(select date1 from Source where Sou_ID=(select max(Sou_ID) from Source)),
(select date2 from Source where Sou_ID=(select max(Sou_ID) from Source))))
where Sou_ID=(select max(Sou_ID) from Source))
"

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-08-13 : 13:29:34
That is correct.

You have written the trigger without considering multi-row dml. You will need to join to the INSERTED table in a FROM clause in your UPDATE statement to make sure you do the UPDATE for all the rows effected by you action.

Jay White
{0}
Go to Top of Page
   

- Advertisement -