I want to transfer all data from one table into another with the following code:
INSERT INTO tblpremier
SELECT * INTO #TempTable
FROM dbo.IntermediateTable
ALTER TABLE #TempTable
DROP COLUMN id
SELECT * FROM #TempTable
And I get an error
Incorrect syntax near the keyword ‘INTO’
at the second line of this code. Any help?.
What you’ve written doesn’t make any sense. The first “statement” is this:
Now are you inserting into #temptable, or tblpremier? I’m guessing you wanted to perform all these operations and then insert into
tblpremier– in which case split it into separate statements. I’m guessing you wanted to do:But rather than need
#Temptablewhich is the same asIntermediateTableminus the ID column, why not just select the correct columns you need fromIntermediateTablein the first place rather than using*?Edit:
Here’s what I meant. Write the insert statement so you’ve got all the column names specified, and don’t include the ID column. You’ll get all new ID numbers on the copy of the table.
However, if you wanted to to keep the same ID numbers in the copy of the table, and the columns are in the same order on both
intermediatetable andtblpremier, then you could write:But you would still need to watch out for trying to insert duplicate IDs if
tblpremierisn’t empty at first.