原先我把@tempBookID长度设置为2,而本来应该是3,所以只有一条记录.......-00,后来我把长度改成3后变成......-000和.....-001两条记录了.
还有其它存储过程跟这个类似,它就能正确添加记录.
create proc Insert_BookStateInfo
@ISDN varchar(10),
@TotalStock integer
as
begin tran
declare @State varchar(10),@BookID varchar(14),@i integer,@tempBookID varchar(3)
set @i=0
set @State='归还'
while (@TotalStock>@i)
begin
set @tempBookID=(convert(varchar(3),(select max(convert(int,right(BookID,len(BookID)-10)))+1
from BookStateInfo
where ISDN=@ISDN )) )
while (len(@tempBookID)<3)
set @tempBookID='0'+@tempBookID
set @BookID=@ISDN+'-'+@tempBookID
insert into BookStateInfo(ISDN,BookID,State)
values(@ISDN,@BookID,@State)
set @i=@i+1
end
update BookInfo
set TotalStock=isnull(TotalStock,0)+@TotalStock
from BookInfo as a ,BookStateInfo as b
where a.ISDN=b.ISDN and b.ISDN=@ISDN
update BookInfo
set BookStock=isnull(BookStock,0)+@TotalStock
from BookInfo as a ,BookStateInfo as b
where a.ISDN=b.ISDN and b.ISDN=@ISDN
commit
GO