Solved

syntax for using a variable when creating a dynamic table name

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
kill process lock Sql server 9 50
SQL query with cast 38 42
SQL Query including math and 2 tables problem 26 50
why sql server only update some statistics in the database ? 3 21
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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