Datta
Datta

Reputation: 849

How to insert record into table result of other Select Query?

My requirement is to insert record from selected query result in to new table.

My query 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')

Now this query result stored in to Temp table. How can I do this?

Upvotes: 0

Views: 57

Answers (3)

Dhruv Pandya
Dhruv Pandya

Reputation: 67

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

Upvotes: 0

Tamil Thomas
Tamil Thomas

Reputation: 1

You can use Cursor also for this case

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;

`

`

Upvotes: 0

shree.pat18
shree.pat18

Reputation: 21757

You can use the Insert Into ... Select idiom like so:

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')

Upvotes: 3

Related Questions