日期:2014-05-18  浏览次数:20528 次

简单的问题请教
create   trigger   delete_employee   on   employee
for   delete  
as  
declare   @typeid   varchar(10)
select   typeid=@typeid   from   deleted
delete   from   loginuser   where   etypeid=@typeid
go

这个触发器里的。select   typeid=@typeid   from   deleted   这句话是什么意思呢,好久都没太明白.deleted是一个表还是什么其它的。


------解决方案--------------------
create trigger delete_employee on employee
for delete
as
declare @typeid varchar(10)
select @typeid =typeid from deleted
delete from loginuser where etypeid=@typeid
go

这个触发器里的。select typeid=@typeid from deleted 这句话是什么意思呢,好久都没太明白.deleted是一个表还是什么其它的。
-----------------------------------------
deleted 是產生的一個臨時表,觸發結束後自動刪除
裡面紀錄的是 表刪除前的數據
select @typeid=typeid from deleted
就是把刪除前的 typeid 值給 變量 @typeid
建議該trigger 這樣寫
create trigger delete_employee on employee
for delete
as
delete loginuser from loginuser ,deleted where loginuser.etypeid=deleted.typeid
go


------解决方案--------------------
create trigger delete_employee on employee
for delete
as
declare @typeid varchar(10)
select typeid=@typeid from deleted
delete from loginuser where etypeid=@typeid
go
-----------------------------------------------
应该:
create trigger delete_employee on employee
for delete --在删除表employee里的记录时触发
as
declare @typeid varchar(10)
select @typeid=typeid from deleted --取得删除记录的typeid
delete from loginuser where etypeid=@typeid --删除loginuser表中etypeid=@typeid的记录
go