create table student(
id int not null,
name varchar(30) collate chinese_prc_ci_as not null,
mark varchar(30) collate chinese_prc_ci_as null
)
go
if exists(select name from sysobjects
where name='tri_test' and type='tr')
drop trigger tri_test
go
create trigger tri_test on dbo.student
instead of insert
as
declare @id_source int
declare @id_target int
declare @name varchar(30)
declare @mark varchar(30)
select @id_target=id,@name=name,@mark=mark from inserted
select @id_source=max(id) from student
if @id_source=0 or @id_source=null
select @id_source=0
if (@id_source+1)!=@id_target
select @id_target=@id_source+1
begin
insert into student values(@id_target,@name,@mark)
if @@rowcount=0
begin
print 'error'
rollback tran
end
end
go