I am new to triggers and i am trying to insert data into a log table after the Alumns table was updated.
| Alumn_ID (PK) | Name | Courses | Favourite_Course
1 Peter 5 3
| Log_ID | Alumn_ID | Note
1 1 Number of courses was changed
For some reason it is not letting me insert the Alumn ID and the trigger doesn't work, also i can't find how to insert the log id as identity
ALTER TRIGGER [LOG]
ON ALUMNS
AFTER UPDATE
AS
BEGIN
DECLARE @Note VARCHAR(50)
DECLARE @Alumn_ID varchar;
SELECT @Alumn_ID= INSERTED.Alumn_ID FROM INSERTED
IF UPDATE(Name)
BEGIN
SET @Note = 'Updated Name'
END
INSERT INTO Alumn_Log (Log_ID, Note)
VALUES (@Alumn_ID, '@Alumn')
END
After that i am trying to modify the name to see if the trigger works and i get the next error:
ERROR:
No row was updated
The data in row 1 was not commited. Error Source: .Net SqlClient Data Provider. Error Message: instruction INSERT is in conflict with restriction FOREIGN KEY "FK_ALUMN_LOG_ALUMN_ID". The conflict has appeared in database "SCHOOL", table "dbo.ALUMNS", Column "ALUMN_ID." Instruction ended.
Correct the errors and retry or press ESC to cancel the change(s).
INSERTEDhas only one record. Everyone does it. What does "not letting me insert the Alumn ID and the trigger doesn't work" mean. Can you describe, with code and error messages, what you are doing and in what way it doesn't work. Please edit your question as this is key to the question