delete触发器
发布网友
发布时间:2022-04-22 10:00
我来回答
共2个回答
热心网友
时间:2023-09-26 12:42
这个我刚做完,我把代码给你
alter trigger tri_transinfo_delete
on transinfo
after delete
as
begin
declare @cardid varchar(5), @oldtime datetime, @monthtime int
select @cardid = cardid, @oldtime = transDate from deleted
set @monthtime = datediff(mm, @oldtime, getdate())
if(@monthtime<1)
begin
print '删除错误!一个月内的记录不能删除!'
rollback
end
else
begin
if exists(select * from sys.objects where [name] = 'transinfo_bak')
begin
insert into transinfo_bak select * from deleted
end
else
begin
select * into transinfo_bak from deleted
end
end
end
go
你要先获得删除的记录的日期,然后比较
最后根据比较返回的值来判断是否删除
给你下面的关键代码
declare @oldtime datetime, @monthtime int
select @oldtime = transDate from deleted
set @monthtime = datediff(mm, @oldtime, getdate())
if(@monthtime<1)
不能删除
else
删除
热心网友
时间:2023-09-26 12:42
transDate 后面少一个右括号,还有你没判断 transDate 为空的情况