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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DELETE trigger

Status
Not open for further replies.

TomBarrand

Programmer
Aug 9, 2000
162
0
0
GB
When a DELETE statement is called on the FOB table I want to delete the FOB_Size records that relate to the FOB record that is trying to be deleted. How do I refer to the FOB.FOB_No that is trying to be deleted? My code is below

CREATE TRIGGER [DELETE_FOB_Size] ON [dbo].[FOB]
FOR DELETE
AS

DELETE FOB_Size
FROM FOB_Size, FOB
WHERE dbo.FOB_Size.FOB_No = dbo.FOB.FOB_No AND
FOB.FOB_No = @ThisFOBNo
 
I'd recommend using the Inserted and Deleted temporary tables created during transactions. On a Delete action, all deleted records appear in the Deleted temp table while the Inserted temp table will be empty.


CREATE TRIGGER [DELETE_FOB_Size] ON [dbo].[FOB]
FOR DELETE
AS

DELETE FOB_Size
FROM FOB_Size INNER JOIN
Deleted ON FOB_Size.FOB_No = Deleted.FOB_No LEFT JOIN
Inserted ON Deleted.FOB_No = Inserted.FOB_No
WHERE Inserted.FOB_No IS NULL

Hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top