随便建一个表,字段以及数据自己编.循环往表里插入1000行数据,并通过事务判断,验证是否插入1000行,是提交,否回滚.
我的表名 books
books表结构如下:
id int (自增长)
bookname varchar(40)
price int
description varchar(50)
amount int
下面是我的代码可结果好像不对,请教
begin tran
declare @i int
set @i=1
while(@i<=1000)
begin
insert into books(bookname,price,descriptions,amount)values('sdfd',rand()*100,'aa',rand()*100)
set @i=@i+1
end
if (@@rowcount!=1000 )
begin
print 'error'
rollback tran
declare @count int
begin tran TEST
select @count=counts from testtable where testid=1
update testtable set counts=@count+1
if (@@error >0) begin
rollback tran TEST
end else begin
commit tran TEST
end