Solved

syntax for using a variable when creating a dynamic table name

Posted on 2011-09-22
3
191 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
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now