为什么删除触发器不能触发?
我在一个表StudentInfo上面建立了一个删除触发器如下
CREATE trigger tr_BackupDeletedData on dbo.StudentInfo
for delete
as
insert StudentInfo_old(studentID, studentNO, studentName, studentGender, studentDepartment, studentGrade, LeaveDate, BuildingNO, FloorNO, RoomNO)
select deleted.studentID, studentNO, studentName, studentGender, studentDepartment, studentGrade, GETDATE(), BuildingNO, FloorNO, RoomNO
from deleted, LodgingRelation, DormInfo
where deleted.studentID = LodgingRelation.StudentID AND LodgingRelation.DormID = DormInfo.DormID
go
结果在StudentInfo中删除一条消息时无法把删除的信息写入到StudentInfo_old表中。
但是,我把这个触发器改成下面这个样子:
CREATE trigger tr_BackupDeletedData on dbo.StudentInfo
for delete
as
insert StudentInfo_old(studentID, studentNO, studentName, studentGender, studentDepartment, studentGrade, LeaveDate)
select deleted.studentID, studentNO, studentName, studentGender, studentDepartment, studentGrade, GETDATE()
from deleted
也就是说仅仅从deleted表中读取信息到StudentInfo_old表中去则是正确的。
求助高手帮忙解释一下为什么?还有,如果sql server不支持在delete触发器中读取其他表信息的话,那么有没有什么好的纷纷能够实现我所要的功能。
------解决方案--------------------select deleted.studentID, studentNO, studentName, studentGender, studentDepartment, studentGrade, GETDATE(), BuildingNO, FloorNO, RoomNO
from deleted, LodgingRelation, DormInfo
查看几个表中是否有其它重名字段出现在字段列表中.
------解决方案--------------------肯定是
select deleted.studentID, studentNO, studentName, studentGender, studentDepartment, studentGrade, GETDATE(), BuildingNO, FloorNO, RoomNO
from deleted, LodgingRelation, DormInfo
where deleted.studentID = LodgingRelation.StudentID AND LodgingRelation.DormID = DormInfo.DormID
这个语句有问题,
或者写入的时候字段写入失败
------解决方案--------------------应该可以的
是你这个句子有问题吧
select deleted.studentID, studentNO, studentName, studentGender, studentDepartment, studentGrade, GETDATE(), BuildingNO, FloorNO, RoomNO
from deleted, LodgingRelation, DormInfo
where deleted.studentID = LodgingRelation.StudentID AND LodgingRelation.DormID = DormInfo.DormID
有没有可能是查出来没有数据呢?