化简SQL语句
本人初学SQL语法,下面是我写的触发器,请教怎样从语句上简化一下。
CREATE TRIGGER identical_insert ON ch_mon
instead of INSERT
AS
if @@rowcount=0 return
declare @tbl varchar(64),@recno bigint,@itype varchar(64),@otype varchar(64)
select @tbl=tbl,@recno=recno,@itype=type from inserted
if not exists(select 1 from ch_mon where recno=@recno and tbl=@tbl)
begin
insert into ch_mon select * from inserted
return
end
select @otype=type from ch_mon where recno=@recno and tbl=@tbl
if @otype='insert' and @itype='delete'
begin
delete from ch_mon where recno=@recno and tbl=@tbl
return
end
if @otype='update' and @itype='delete'
update ch_mon set type='delete'where recno=@recno and tbl=@tbl
go