大虾们帮我看看这个问题,求解释。。。
我们做数据库实验,关于触发器和存储过程的.
题目是:对employee表写一个允许用户一次只删除一条记录的触发器。
我写的代码是:
create trigger delete_one
on employee
for delete
as
begin
declare @delete_count int
select @delete_count=COUNT(*) from deleted
if @delete_count>1
begin
print('一次不能删除多个值!')
rollback
end
end
go
而我看到我一标准答案是:
Create trigger tri_update_emp on employee
For update
As
Declare @oldbir datetime,@newbir datetime,@oldhir datetime,@newhir datetime, @emp_no char(5)
Select @oldbir=birthday,@oldhir=date_hired from deleted
Select @newbir=birthday,@newhir=date_hired from inserted
If((datediff(day,@newbir,@oldhir)>0 or datediff(day,@newbir,@newhir)>0 or datediff(day,@oldbir,@oldhir)>0))
begin
If(datediff(year,@newbir,getdate())>=25)
begin
If(datediff(year,@newbir,@newhir)>20)
begin
Update employee set birthday=@newbir,date_hired=@newhir where emp_no=@emp_no
end
Else
begin
Print('年龄间隔不够年,不合常理');
Rollback transaction
end
end
Else
begin
Update employee set birthday=@newbir,date_hired=@newhir where emp_no=@emp_no
end
end
Else
begin
Print('时间先后有误,有违常理');
rollback transaction
end
go
里面我想问一下,我觉得只要写不满足条件时系统只要rollback就行,而标准答案还要加上满足条件是该怎样执行的代码,请问有这个必要写满足条件时该怎么执行的代码吗?
------解决方案--------------------
SQL code
create trigger delete_one
on employee
instead of delete
as
begin
declare @delete_count int
select @delete_count=COUNT(*) from deleted
if @delete_count>1
begin
insert employee
select * from deleted
print('一次不能删除多个值!')
rollback
end
end
go
--什么标准答案?不用理会,按照自己的逻辑写,能实现就好了。跳跳大路通罗马