June 5, 2007 at 8:33 am
FOR INSERT, UPDATE
AS
If Update(Inactive)
BEGIN
If Inactive = 1
BEGIN
DELETE ContactResp WHERE kContactNameID In(SELECT ContactNameID FROM Inserted)
END
END
If Update(Inactive)
BEGIN
DELETE ContactResp WHERE kContactNameID In(SELECT ContactNameID FROM Inserted WHERE Inactive = 1)
END
June 5, 2007 at 11:14 am
It is my understanding that the if UPDATE(columnname) evalutes to true if any record in the result set had that column modified/inserted. So your delete statement would include all rows where the inactive column value equals 1 whether it changed or not, unless none of the records had that column value change.
However your can use the following:
DELETE ContactResp
WHERE kContactNameID In(SELECT ContactNameID
FROM Inserted i Join contactnames cn on (i.pk = cn.pk)
WHERE i.inactive = 1 and cn.Inactive <> 1)
putting it inside an IF UPDATE() statement just prevents it from executing unnecessarily, but even outside that statement it will still accomplish exactly what you want. It joins the "inserted" table back to the base table and finds only where inactive has changed to 1.
HTH,
James.
June 6, 2007 at 2:24 am
Cheers James very helpful - just one think what is PK?
i.pk = cn.pk?
Thats the primary key for the record? and that join only brings out those in i and joins the relevent cn records?
Thanks again
June 6, 2007 at 6:29 am
Sorry, PK is short for "Primary Key" (if the table PK is a composit of two or more columns adjust the join statement accordingly)
Since I did not know the actual name of the PK column in your table I just used "PK".
Also I may have been to hasty in my answer, it's been a few months since I worked with triggers. I think you may want to join to the "deleted" table rather than the base table. The deleted table holds the "Original" records while the base table has already been altered. Give me a minute to test something and I'll post updated code.
James.
June 6, 2007 at 7:00 am
Ok, I wanted to make sure. Yes you need to alter the code I posted as follows:
DELETE ContactResp
WHERE kContactNameID IN (SELECT ContactNameID
FROM inserted i JOIN deleted d on (i.pk = d.pk)
WHERE i.inactive = 1 and d.inactive <> 1)
Another consideration is newly inserted records. If you want to act on those records then the following should be considered:
--When "deleted" is empty then an insert occurred
--When "inserted" is empty a delete occurred
--When neither is empty then an update occurred
if exists (select 1 from inserted) and not exists (select 1 from deleted)
begin
DELETE ContactResp
WHERE kContactNameID IN(SELECT ContactNameID FROM deleted d WHERE d.inactive = 1)
end
--James.
June 6, 2007 at 7:15 am
OK Thanks - I think I have the update part OK.
But the insert bit -
Another consideration is newly inserted records. If you want to act on those records then the following should be considered:
--When "deleted" is empty then an insert occurred
--When "inserted" is empty a delete occurred
--When neither is empty then an update occurred
if exists (select 1 from inserted) and not exists (select 1 from deleted)
begin
DELETE ContactResp
WHERE kContactNameID IN(SELECT ContactNameID FROM deleted d WHERE d.inactive = 1)
end
Should this be - FROM inserted i where i.inactive = 1
??
Cheers
June 6, 2007 at 7:25 am
Oops!
Your right switch back to the "inserted" table. That what I get for not reviewing what I just posted. I had the "deleted" table on my mind, as it should be empty during inserts. Just goes to show you how important thorough testing is (or at least code review sessions!).
James.
June 6, 2007 at 7:44 am
Excellent replies . Thankyou
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply