在sql server 2005中将数据从一个表插入另一个表

时间:2013-11-13 02:01:10

标签: sql-server-2005

使用下表

table1(id,questionid,response,points)

1,1,'yes',4

2,1,'no',4

3,1,'copper',2

4,2,'yes',1

5,2,'yes',2

希望将table1中的数据插入table2(id,ques1,point1,ques2,point2,ques3,point3,ques4,point4,ques5,point5)

插入响应,将值指向相应的question1d's

如果questionid = 1

insert into table2 values ques1='yes' and points=4

table2输出

id,ques1,point1,ques2,point2,ques3,point3,ques4,point4,ques5,point5

1  'yes'  4      'yes'  2      'no'  2    ..........

1 个答案:

答案 0 :(得分:0)

你的样本似乎有一两个错误,但我认为你想要类似的东西:

SELECT id,
    q1.response, q1.points,
    q2.response, q2.points,
    q3.response, q3.points,
    q4.response, q4.points
    q5.response, q5.points
INTO table2
FROM table1 q1
    JOIN table1 q2 ON q1.questionid = q2.questionid
    JOIN table1 q3 ON q2.questionid = q3.questionid
    JOIN table1 q4 ON q3.questionid = q4.questionid
    JOIN table1 q5 ON q4.questionid = q5.questionid
WHERE q1.id = 1 AND q2.id = 2 AND q3.id = 3 AND q4.id = 4 AND q5.id = 5