0
我有一個問卷存儲過程,用於更新問題,可能的答案和給出的答案。我遇到的問題是如果問題是真/假或是/否問題,或者有4個可能答案的多項選擇問題。存儲過程中的不同或相同變量
對於每個可能的答案,我是否需要一個新的變量名稱,如@ PossibleANswert1,@ PossibleAnswer2或者我可以只有一個嗎?
我有這個至今:
BEGIN
IF @Question_Type = 1 //a 1 means its a two answer question otherwise it will be a 4 answer question
BEGIN
Insert into QUESTIONS
(QuestionType, Question_Text, Questionaire_ID, Filter)--
Values
(@Question_Type, @New_Question, @Questionnaire_ID, @Filter)
select @NewQuestion_ID = SCOPE_IDENTITY()--
insert into Possible_Answers
(Question_ID, Explanation_Required, Review_Required, Possible_Answer_Text, Question_Type)
values
(@NewQuestion_ID, @Explanation,@Review,@Possible_Answer,@Question_Type), // true/false or yes/no
(@NewQuestion_ID, @Explanation,@Review,@Possible_Answer,@Question_Type)
insert into GIVEN_ANSWERS
(Question_ID, Answer_Text, Explanation_Text, Sample_ID)
values
(@NewQuestion_ID, @Given_Answer, @Explanation_Text, @Sample_ID)
END
ELSE
BEGIN
Insert into Questions
(QuestionType, Question_Text, Questionaire_ID, Filter)--
Values
(@Question_Type, @New_Question, @Questionnaire_ID, @Filter)
select @NewQuestion_ID = SCOPE_IDENTITY()
insert into Possible_Answers
(Question_ID, Explanation_Required, Review_Required, Possible_Answer_Text, Question_Type)
values
(@NewQuestion_ID, @Explanation,@Review,@Possible_Answer,@Question_Type), // mulitple choice
(@NewQuestion_ID, @Explanation,@Review,@Possible_Answer,@Question_Type),
(@NewQuestion_ID, @Explanation,@Review,@Possible_Answer,@Question_Type),
(@NewQuestion_ID, @Explanation,@Review,@Possible_Answer,@Question_Type)
insert into GIVEN_ANSWERS
(Question_ID, Answer_Text, Explanation_Text, Sample_ID)
values
(@NewQuestion_ID, @Given_Answer, @Explanation_Text, @Sample_ID)
END
END
我是相當新的存儲過程,所以請隨時撥打任何其他錯誤或糟糕的編碼看到
謝謝,不知道爲什麼,我得到downvoted,但感謝你回答,我要試試這個,也是一個朋友建議有可能的答案的XML表,所以我要尋找到雙方 – 2014-10-29 15:25:03
根據您的情況,用戶定義的類型通常更容易處理,但XML文檔更加靈活。 – Will 2014-10-29 15:27:56