I have tables named Categories
, Questions
and Selections
. The relationship of the tables are: there can be 1 or more Selections
in a Question
and there can be one or more Questions
in a Category
.
The table columns are the following:
Categories
- CategoryID (pk)
- NameQuestions
- QuestionID (pk)
- Question
- CategoryID (fk)Selections
- SelectionID (pk)
- Selection
- QuestionID (fk)
I want to convert this code from C# to SQL:
private int fromCategoryID = 1;
private int toCategoryID = 2;
Category cat1 = new Category(); //this is the category we will get questions from.
Category cat2 = new Category(); //this is the category we copy the questions to.
// code to populate the 2 category instances and their children (Questions) and
// children's children (Selections) removed for brevity.
// copy questions and selections from cat1 to cat2
foreach(Question q from cat1.Questions)
{
Question newQuestion = new Question();
newQuestion.Question = q.Question;
foreach(Selection s in q.Selections)
{
Selection newSelection = new Selection();
newSelection.Selection = s.Selection;
q.Selections.Add(newSelection);
}
cat2.Questions.Add(newQuestion);
}
How can this be done in SQL?