由于已解决的问题不同,我之前发布了此存储过程。但是我注意到,当我执行存储过程并给它从其他代码接收的值时,它只插入测验表而不是其他2个表,即游戏和视频。
这是存储过程
CREATE PROCEDURE s_Add$Subject$Info
(
@SubjectName varchar(50),
@SubjectDescription varchar(max),
@QuizIDString varchar(100),
@GamesIDString varchar(100),
@VideoIDString varchar(100)
)
as
--declare variables
declare @theSubID int
declare @quizIDLength int
declare @gamesIDLength int
declare @videoIDLength int
declare @quizIDs varchar(200)
declare @gamesIDs varchar(200)
declare @videoIDs varchar(200)
declare @numberCount int
declare @sTemp varchar(100)
--initializing variables
set @quizIDLength = LEN(@QuizIDString)
set @gamesIDLength = LEN(@GamesIDString)
set @videoIDLength = LEN(@VideoIDString)
set @quizIDs = ''
set @gamesIDs = ''
set @videoIDs = ''
set @numberCount = 1
--declare temp tables
declare @Q table --Quiz
(
QuizID varchar(100),
SubjectID int
)
declare @G table --Games
(
GamesID varchar(100),
SubjectID int
)
declare @V table --Videos
(
VideoID varchar(100),
SubjectID int
)
insert into[Subjects]
(SubjectName, SubjectDescription)
values
(@SubjectName, @SubjectDescription)
set @TheSubID = @@IDENTITY
--Check to see what strings are full then entering ids if true
--Quizzes first
if(@QuizIDLength > 0)
begin
while(@numberCount <=@quizIDLength)
begin
set @sTemp = SUBSTRING(@QuizIDString, @numberCount, 1)
if(@sTemp = ',')
begin
insert into @Q (QuizID, SubjectID) values (@quizIDs, @theSubID)
set @quizIDs = ''
end
if(@sTemp <> ',')
begin
set @quizIDs = @quizIDs + @sTemp
end
set @numberCount = @numberCount + 1
end
end
--Games second
if(@gamesIDLength > 0)
begin
while(@numberCount <=@gamesIDLength)
begin
set @sTemp = SUBSTRING(@GamesIDString, @numberCount, 1)
if(@sTemp = ',')
begin
insert into @G (GamesID, SubjectID) values (@gamesIDs, @theSubID)
set @gamesIDs = ''
end
if(@sTemp <> ',')
begin
set @gamesIDs = @gamesIDs + @sTemp
end
set @numberCount = @numberCount + 1
end
end
--Videos third
if(@videoIDLength > 0)
begin
while(@numberCount <=@videoIDLength)
begin
set @sTemp = SUBSTRING(@VideoIDString, @numberCount, 1)
if(@sTemp = ',')
begin
insert into @V (VideoID, SubjectID) values (@videoIDs, @theSubID)
set @videoIDs = ''
end
if(@sTemp <> ',')
begin
set @videoIDs = @videoIDs + @sTemp
end
set @numberCount = @numberCount + 1
end
end
--inserting the ids to subjectinfo table
insert into [SubjectInfo]
(SubjectID, QuizID)
select SubjectID, QuizID
from @Q
--Games
insert into [SubjectInfo]
(SubjectID, GameID)
select SubjectID, GamesID
from @G
--Video
insert into [SubjectInfo]
(SubjectID, VideoID)
select SubjectID, VideoID
from @V
我已经尝试将插入插入其各自的if语句中,我甚至尝试使用内连接一次性执行插入操作,但它确实没有用到。
那么为什么数据只会在第一次插入时插入,使用我声明的表,而不是其他? 我可以通过用C#/ ASP.NET编码来解决这个问题,但是我必须多次打开一个连接,我真的不想这样做,因为我已经编写了存储过程来插入我需要的东西我在撰写时也可以在存储过程中处理它。
任何想法或任何人都可以看到我做错了什么?