主题:delete触发器用法一题
/*transinfo--交易信息表*/
if exists(select * from sysobjects where name = 'trig_delete_transinfo')
drop trigger trig_delete_transinfo
go
/*创建delete触发器*/
create trigger trig_delete_transinfo
on transinfo
for delete
as
print'开始备份数据,请稍后...'
declare @date int
select @date=datediff(day,transDate,getdate()) from transinfo
if @date>30
begin
if not exists(select * from sysobjects where name = 'backupTable')
select * into backupTable from deleted
else
insert into backupTable select * from deleted
print'备份成功!备份表中的数据为:' select * from backupTable
end
else
begin
print'删除失败'
raiserror('警告:不能删除一个月以内的数据!',16,1)
rollback transaction
end
go
/*测试触发器*/
set nocount on
delete transinfo
print'交易信息表中的数据为:'
select * from transinfo
以上delete触发器的创建过程.
我的操作方法:
首先将系统时间改成2个月前,然后在transinfoz表中输入一条记录.再将系统时间改为当前时间.执行delete transinfo.显示如下:
服务器: 消息 50000,级别 16,状态 1,过程 trig_delete_transinfo,行 20
警告:不能删除一个月以内的数据!
开始备份数据,请稍后...
删除失败
我的本意应该是执行if语句,怎么会执行else语句?求助...
if exists(select * from sysobjects where name = 'trig_delete_transinfo')
drop trigger trig_delete_transinfo
go
/*创建delete触发器*/
create trigger trig_delete_transinfo
on transinfo
for delete
as
print'开始备份数据,请稍后...'
declare @date int
select @date=datediff(day,transDate,getdate()) from transinfo
if @date>30
begin
if not exists(select * from sysobjects where name = 'backupTable')
select * into backupTable from deleted
else
insert into backupTable select * from deleted
print'备份成功!备份表中的数据为:' select * from backupTable
end
else
begin
print'删除失败'
raiserror('警告:不能删除一个月以内的数据!',16,1)
rollback transaction
end
go
/*测试触发器*/
set nocount on
delete transinfo
print'交易信息表中的数据为:'
select * from transinfo
以上delete触发器的创建过程.
我的操作方法:
首先将系统时间改成2个月前,然后在transinfoz表中输入一条记录.再将系统时间改为当前时间.执行delete transinfo.显示如下:
服务器: 消息 50000,级别 16,状态 1,过程 trig_delete_transinfo,行 20
警告:不能删除一个月以内的数据!
开始备份数据,请稍后...
删除失败
我的本意应该是执行if语句,怎么会执行else语句?求助...