?
Solved

syntax for using a variable when creating a dynamic table name

Posted on 2011-09-22
3
Medium Priority
?
237 Views
Last Modified: 2012-05-12
Hi,
I'm using sql server 2005 and I want to create a table titled Step_1 appended with the value contained in the variable @start_date....
Having a bit of trouble with the syntax below,
any help appreciated...thanks

@start_date datetime ,
@end_date datetime

Select * into Step_1'+@start_load_date+'FROM
      (SELECT *
from
Data_Table
WHERE LoadDate BETWEEN @start_load_date AND @end_load_date)A

0
Comment
Question by:blossompark
[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
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 36579766
the only way to do that is dynamic sql
@start_date datetime , 
@end_date datetime

DECLARE @sql nvarchar(4000)

set @sql = '
Select * 
into [Step_1' + convert(varchar(10), @start_date, 120) + '] FROM
     (SELECT * from Data_Table
   WHERE LoadDate BETWEEN @v_start_date AND @v_end_date)A '
exec sp_executesql @sql, N'@v_start_date datetime, @v_end_date datetime, @start_date, @end_date

Open in new window

0
 

Author Comment

by:blossompark
ID: 36580034
Hi Angellll,
thank you for that...will implement and update you
0
 

Author Closing Comment

by:blossompark
ID: 36580421
Hi Angellll,
thank you for that, does the job!
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 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