SQL SERVER触发器使用实例

--级联删除(不带外键)
create trigger dept_emp_delete
on dept
after delete
as
delete from emp
where deptid in
(select id from deleted)
Go

--限制插入
create trigger dept_emp_insert
on emp
after insert
as
if(select inserted.deptid from dept,inserted where dept.id = inserted.deptid) = 2
begin
   print 'the emp is full'
   rollback transaction
end
go

insert into emp values ('xiaowang',2,'2700')

你可能感兴趣的:(SQL Server)