if object_id('insertAllRecord') is not null
drop procedure insertAllRecord
go
create procedure insertAllRecord
@UserName varchar(20),
@Sex varchar(2),
@Class varchar(20),
@old int
as
declare @NO int,@SystemTime DateTime
select @NO=(select Max(学号) from student)
select @NO=@NO+1
select @SystemTime=GetDate()
begin transaction insertRecord
insert into student (姓名,性别,班级,年龄) values(@UserName,@Sex,@Class,@old)
if @@rowcount=0
begin
rollback work
return
end
insert into homework (学号,提交日期) values(@NO,@SystemTime)
if @@rowcount=0
begin
rollback work
return
end
insert into testProc values(@NO)
if @@error<>0
begin
rollback work
return
end
commit transaction insertRecord
go
drop procedure insertAllRecord
go
create procedure insertAllRecord
@UserName varchar(20),
@Sex varchar(2),
@Class varchar(20),
@old int
as
declare @NO int,@SystemTime DateTime
select @NO=(select Max(学号) from student)
select @NO=@NO+1
select @SystemTime=GetDate()
begin transaction insertRecord
insert into student (姓名,性别,班级,年龄) values(@UserName,@Sex,@Class,@old)
if @@rowcount=0
begin
rollback work
return
end
insert into homework (学号,提交日期) values(@NO,@SystemTime)
if @@rowcount=0
begin
rollback work
return
end
insert into testProc values(@NO)
if @@error<>0
begin
rollback work
return
end
commit transaction insertRecord
go
在三个表(student,homework,testProc)中插入三条记录,如果其中一条插入出现问题,则回滚事务。请各位帮忙!
drop procedure insertAllRecord
go
create procedure insertAllRecord
@UserName varchar(20),
@Sex varchar(2),
@Class varchar(20),
@old int
as
declare @NO int,@SystemTime DateTime
select @NO=(select Max(学号) from student)
select @NO=@NO+1
select @SystemTime=GetDate()
set xact_abort on --******** 加一句,这样出错时会自动回滚,因为你前面的插入都没有做错误处理,其他好像没有什么问题
begin transaction insertRecord
insert into student (姓名,性别,班级,年龄) values(@UserName,@Sex,@Class,@old)
if @@rowcount=0
begin
rollback work
return
end
insert into homework (学号,提交日期) values(@NO,@SystemTime)
if @@rowcount=0
begin
rollback work
return
end
insert into testProc values(@NO)
if @@error<>0
begin
rollback work
return
end
commit transaction insertRecord
go