Solved

Pass var to table name

Posted on 2013-01-10
3
312 Views
Last Modified: 2013-01-16
Is it possible to pass a var into the table name? i.e.:

/**************************/
declare @monthEnd varchar(8) = '12312012'

select
   *
into
   tableName_@monthEnd
/**************************/

So "tableName_12312012" is the actual table name.

Thanks!
0
Comment
Question by:Rickzzz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 400 total points
ID: 38765000
You could do it with dynamic sql

declare @monthend varchar(8)
declare @sql nvarchar(4000)

set @monthend = '12312012'

set @sql = 'select * into tableName_'+@monthEnd+' from whereever'
exec (@sql)

Open in new window


Talked to Mr Google and found an example: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=68177 - with a link to a very long blog post on dynamic SQL.
0
 
LVL 27

Assisted Solution

by:Chris Luttrell
Chris Luttrell earned 100 total points
ID: 38765237
The only way to do it is with dynamic sql. You can not substitue what would be SYSNAMEs, i.e. Tables, Columns, Schemas, with variables.  You have to build the SQL statement as a string and execute it with exec(@sql) or the prefered system procedure EXECUTE_SQL @sql, see Kimberly L. Tripp's article on Execute_SQL
0
 

Author Closing Comment

by:Rickzzz
ID: 38784496
Thanks!
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 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