2014-10-29 64 views
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 

我是相當新的存儲過程,所以請隨時撥打任何其他錯誤或糟糕的編碼看到

回答

0

我相信你在找什麼for是一個表用戶定義的類型。這將允許您通過任意數量的列傳遞任意數量的可能答案。

create type dbo.possible_answer as table(
    answer nvarchar(max), 
    explaination nvarchar(max), 
    review nvarchar(max) 
); 

create procedure dbo.create_question 
@QuestionType int, 
....(Other params here) 
@PossibleAnswers dbo.possible_answer readonly 
as 
begin 
....(some code) 
    insert into Possible_Answers 
    (Question_ID, Explanation_Required, Review_Required, Possible_Answer_Text, Question_Type) 
    select @NewQuestion_ID, 
    p.explaination, 
    p.review, 
    p.answer, 
    @Question_Type 
    from dbo.possible_answer p; 
...(other code) 
end 
+0

謝謝,不知道爲什麼,我得到downvoted,但感謝你回答,我要試試這個,也是一個朋友建議有可能的答案的XML表,所以我要尋找到雙方 – 2014-10-29 15:25:03

+0

根據您的情況,用戶定義的類型通常更容易處理,但XML文檔更加靈活。 – Will 2014-10-29 15:27:56

相關問題