0
值以下select語句slects的更新結果基於測試ID測試ID的循環儘管SQL
select testID from tblTests
我的代碼的打擊名單。我想循環通過上面的代碼,以便我可以一次循環一個ID。我如何循環所有的測試ID從上面的聲明?
--get decision from tbloptions
declare @decision nvarchar
declare @newQuestionNo char(10)
declare @suffix char(1)
declare @scenarioID int
declare @scenarioNo int
declare @testsummaryid int
declare @points int
select
@decision = tbloptions.decision,
@newQuestionNo = rtrim(tbloptions.GoToQuestion),
@points = coalesce(tbloptions.points,0),
@suffix = tbloptions.GoToSuffix,
@scenarioID = tbloptions.scenarioID,
@testsummaryid = testsummaryid
from
tbloptions with(nolock)
join tbltests on tbloptions.scenarioID = tbltests.scenarioID
and tbloptions.questionNo = tbltests.questionNo
where
tbltests.testid =
if @newQuestionNo in ('Pass','Neutral','Fail')
begin
--end scenario session
update tblTestSummaries
set
end_date = getdate(),
points = @points,
completed = 1,
timetaken = dbo.TimeTaken(
(select
start_date
from tbltestsummaries
where testsummaryid = @testsummaryid),
getdate()),
result = case
when @points > 2 then 'P'
when @points = 2 then 'I'
when @points < 2 then 'F'
end,
testSessionID = @testSessionID
where testsummaryid = @testsummaryid
end
更改'where tbltests.testid ='to'where tbltests.testid IN(select testID from tblTests)''。歡呼!不需要循環! –
如果你必須循環,那麼你將需要創建一個基於你的SELECT語句的CURSOR和循環使用光標 – Mike
我沒有添加所有的conde – user3636426