SQLTeam.com | Weblogs | Forums

Incorrect syntax near the keyword 'INTO' in dynamic query

I'm trying to execute a query that is dynamic SQL and use SELECT INTO within the query to put in a temp table. Rather annoyingly, it worked perfectly once, then started throwing the error: Incorrect syntax near the keyword 'INTO'

And I can't see why it is suddenly doing this?

I'am putting the dynamic SQL into a variable then running it with a stored proc that executes statements.

LIke this:

===========================
if OBJECT_ID('tempdb..#Temp') IS NOT NULL DROP table #Temp

Create Table #Temp
(Col1
, Col2
, col3
, col4
, col5
, col6
, col)

@sql = N'Select Col1, col2, col3, col4, col5, col6
FROM Table1
INTO #Temp'

EXECUTE sp_runquery @sql

Any ideas greatly appreciated.

Changed it to INSERT INTO in the query and it seems to be working better. I think there is a rule about using select into in a dynamic query.