如何将记录插入到其他Select Query的表结果中?

时间:2014-03-21 04:03:47

标签: sql database

我的要求是将所选查询结果中的记录插入到新表中。

我的查询是

SELECT        
   a.Section_name, b.sectionname, a.SubQno, b.Qno, a.ans, b.Answ, 
   a.Exame_id, b.Exame_id AS Expr1, b.User_id, b.Start_time, b.End_time
FROM Question AS a
INNER JOIN Solve_Student_question AS b ON a.SubQno = b.Qno AND a.Section_name = b.sectionname
WHERE (b.User_id = 'gopal ram51765078')

现在此查询结果存储在Temp表中。我怎么能这样做?

3 个答案:

答案 0 :(得分:3)

您可以使用Insert Into ... Select这样的习语:

INSERT INTO TempTable
(...) --Columns
SELECT        
a.Section_name, b.sectionname, a.SubQno, b.Qno, a.ans, b.Answ, a.Exame_id, b.Exame_id AS Expr1, b.User_id, b.Start_time, b.End_time
FROM Question AS a INNER JOIN
Solve_Student_question AS b ON a.SubQno = b.Qno AND a.Section_name = b.sectionname
WHERE (b.User_id = 'gopal ram51765078')

答案 1 :(得分:0)

insert into [temptablename] (comma-separated list of column names) 
   (your Select Query Here)

答案 2 :(得分:0)

对于这种情况,您也可以使用Cursor

declare
cursor C is
select a.Section_name, b.sectionname, a.SubQno, b.Qno, a.ans, 
b.Answ, a.Exame_id,    b.Exame_id AS Expr1, b.User_id, b.Start_time, b.End_time
FROM Question AS a INNER JOIN
Solve_Student_question AS b ON a.SubQno = b.Qno AND a.Section_name = b.sectionname
WHERE (b.User_id = 'gopal ram51765078');
begin
for i in C
insert into tablename(Column1,Column2,Column3)
        values(i.column1,i.Column2,i.Column3);
end loop;
Exit when last.record = 'TRUE';
end;

`

`