我有表名爲Categories
,Questions
和Selections
。這些表格的關係是:在Question
中可以有一個或多個Selections
,並且在Category
中可以有一個或多個Questions
。如何使用T-SQL複製數據?
表列如下:
分類
- 類別ID(PK)
- 名稱問題
- QuestionID(PK)
- 問題
- 類別ID( fk)選擇
- SelectionID(PK)
- 選擇
- QuestionID(FK)
我想從C#這個代碼轉換爲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);
}
如何才能做到這一點在SQL中?
我們在這裏談論的是什麼樣的PK? – 2009-06-11 05:50:07
您的意思是PK的數據類型?這是一個身份播種PK(int)。 – jerbersoft 2009-06-11 06:29:09
什麼版本的SQL Server? – HLGEM 2009-06-11 13:24:06