Solved

syntax for using a variable when creating a dynamic table name

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

696 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