Solved

create a new table with a variable name

Posted on 2011-09-14
5
294 Views
Last Modified: 2012-05-12
i keep getting "Msg 102, Level 15, State 1, Line 26 Incorrect syntax near '@newtbl'."

for

SELECT * INTO @newtbl FROM TMP8.dbo.tblQuery8

please help. thanks!
0
Comment
Question by:ironpen45
5 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 36537387
you have to use dynamic SQL.

check "Dynamic SQL in a stored procedure" from http://www.sqlteam.com/article/introduction-to-dynamic-sql-part-1
0
 
LVL 4

Expert Comment

by:manishkungwani
ID: 36537389
The query is not correct, can you state what you want to do, so that I can suggest the right query for you.
0
 

Expert Comment

by:wenogk
ID: 36537410
Give me more into what you would like to do with this query and i'll just give you the code
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 36537425

Declare @SQL VarChar(1000)

SELECT @SQL = 'SELECT * INTO ' + @newtbl + ' FROM TMP8.dbo.tblQuery8'

Exec ( @SQL)
0
 

Author Closing Comment

by:ironpen45
ID: 36537443
thanks!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Webservices in T-SQL 3 31
sql server insert 12 30
SQL Improvement  ( Speed) 14 28
Unable to Uninstall Visual Studio 2015 7 27
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question