Delete trigger

Posted by Neeks under Sql Server on 2/8/2009 | Views : 40539 | Status : [Member] | Replies : 2
I am deleting a row from a table say student table. I am writing a trigger which will insert the details related to the deleted row in another table say student_log table.
Now I want to store the details related to RollNo, Date when the the student was deleted.


Table Details:
Student:
RollNo (int/PK), FName, LName
student_log
id (int/PK), date_deleted, RollNo(PK of Student table)

Thanks in Advance.




Responses

Posted by: Majith on: 2/8/2009 [Member] Starter

Up
0
Down
CREATE TRIGGER del_Student
ON Student
FOR DELETE AS

IF @@rowcount = 0 RETURN

UPDATE Student_Log
SET Id = 'Value' and date_deleted='Value' and RollNo='Value'
WHERE Id IN (SELECT Id FROM Student)

When ever the delete operations are occuring the value can be stored in the Student_log Table.
Let me know if you have any Queries in the trigger.

Neeks, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Deeraj on: 2/10/2009 [Member] Starter

Up
0
Down
CREATE TRIGGER [dbo].[Student_Update]
ON [dbo].[Student]
AFTER DELETE
AS
BEGIN
INSERT INTO Student_Log SELECT <columns> FROM DELETED
END

HTH

The above version is compatible with SQL 2005.

Neeks, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response