我有一个调查问卷的存储过程,可以更新问题,可能的答案和给出的答案。我遇到的问题是问题是真/假或是/否问题,还是有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 :(得分:0)
我相信你要找的是用户定义的表类型。这将允许您使用任意数量的列传递任意数量的可能答案。
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