Solved

syntax for using a variable when creating a dynamic table name

Posted on 2011-09-22
3
203 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Date Query 28 44
Script to randomly create characters in MS SQL 6 30
Caste datetime 2 51
Getting same value for every field in SQL 2 21
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

15 Experts available now in Live!

Get 1:1 Help Now