自己写的第一个存储过程
alter procedure sp_LevelsCreate
@BookID int,
@WordCount int,
@LevelsCount int
as
declare @curID int
declare @BookWordID int
declare @WordRange int
declare @curLevelsWordsCount int
declare @i int
declare @j int
begin tran
update T_Levels_Words set IsDelete=1 where LevelsID in(select ID from T_Levels where BookID=@BookID )
update T_Levels set IsDelete=1 where BookID=@BookID
set @i=1
while @i<=@LevelsCount
begin
set @curLevelsWordsCount=0
if @i=@LevelsCount
begin
insert into T_Levels(BookID,Name,Levels,WordsCount,PassedWordCount,IsDelete)
values(@BookID,'第'+Convert(varchar(50),@i)+'关',@i,@WordCount/@LevelsCount+@WordCount%@LevelsCount,Convert(int,(@WordCount/@LevelsCount+@WordCount%@LevelsCount)*0.75),0)
set @curLevelsWordsCount=@WordCount/@LevelsCount+@WordCount%@LevelsCount
end
else
begin
insert into T_Levels(BookID,Name,Levels,WordsCount,PassedWordCount,IsDelete)
values(@BookID,'第'+Convert(varchar(50),@i)+'关',@i,@WordCount/@LevelsCount,Convert(int,@WordCount/@LevelsCount*0.75),0)
set @curLevelsWordsCount=@WordCount/@LevelsCount
end
select @curID=max(ID) from T_Levels
------------------------插入T_Levels_Words
set @j=1
while @j<=@curLevelsWordsCount
begin
select ID, row_number() over(order by OrderIndex) as xuhao from T_Book_Words where BookID=@BookID and id not in
(select BookWordID from T_Levels_Words where LevelsID in(select ID from T_Levels where BookID=@BookID and IsDelete=0))
set @WordRange=@@rowcount
select @BookWordID=ID from (select ID,row_number() over(order by OrderIndex) as xuhao from T_Book_Words where BookID=@BookID and id not in
(select BookWordID from T_Levels_Words where LevelsID in(select ID from T_Levels where BookID=@BookID and IsDelete=0)))t where xuhao=ceiling(rand()*@WordRange)
insert into T_Levels_Words(LevelsID,BookWordID,OrderIndex,IsDelete)
values(@curID,@BookWordID,100,0)
set @j=@j+1
end
----------------------------------------
set @i=@i+1
end
---if @@error<>0 GOTO Error1
commit tran
--Error1:
--rollback tran
--return
exec sp_LevelsCreate 1140,32,3
select * from T_Levels where isdelete=0
select * from T_Levels_Words where isdelete=0