我有表名为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