I have several nested insert commands. Some of the nested loops share redundant code. Should I make the redundant code its own loop, or create separate instances of the same code within each loop?
EXAMPLE (edited for clarification):
--Questions 32<->37
SET @index=0
SET @values = 'at your primary grocery store^at WalMart or Sam''s Club^at any other chain (e.g. Target, K-Mart)^in general'
IF SUBSTRING(@values, LEN(@values), 1) <> '^' SET @values = @values + '^'
WHILE (LEN(@values)<>0)
BEGIN
SET @index=CHARINDEX('^', @values)
SET @result=SUBSTRING(@values, 0, @index)
SET @values=SUBSTRING(@values, LEN(@result)+2, LEN(@values)-LEN(@result)-1)
SET @question = 'How much do you spend <b>'+@result+'</b> per trip compared to this time last year?'
SET @qnum=@qnum+1
INSERT INTO checklist_questions (
checklist_id
,checklist_question_id
,checklist_answer_category_id
,autofail_flag
,checklist_responsible_type_id
,correction_days
,checklist_question_header_id
,question
)
VALUES (
@checklist_id
,@qnum --question #
,40 --answer category id
,0 --autofail flag
,'P' --checklist_responsible_type_id
,27 --correction_days
,4 --correction_days
,@question
)
SET @i=1
WHILE (@i<=6)
BEGIN
INSERT INTO checklist_answers (
checklist_id
,checklist_question_id
,checklist_answer_category_id
,checklist_answer_type_id
,detail_flag
)
VALUES (
@checklist_id
,@qnum --question number
,38 --category
,@i --answer type
,0 --detail flag
)
SET @i=@i+1
END
END
The same pattern is repeated over and over, with different values for @values and @question.