ash_dev15 ash_dev15 - 6 months ago 9
SQL Question

Error when inserting data in temporary table (sql)

I am trying to create a temporary table and add information from another table into it. However I keep getting the error


You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'SELECT course_name, ins_name FROM courses where
course_name='ACCA'' at line 2'


Can somebody tell me what I am doing wrong?

Here is my code:

INSERT INTO #TempTable (course, ins)
SELECT course_name, ins_name
FROM courses
where course_name = 'ACCA'

Answer

You should create the temp table first

 CREATE TEMPORARY TABLE 
 IF NOT EXISTS your_table  AS (SELECT course_name, ins_name
             FROM courses 
             where course_name='ACCA');