Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Dynamic table name with getDate()

Author  Topic 

bubberz
Constraint Violating Yak Guru

289 Posts

Posted - 2008-06-17 : 19:50:09
I'm trying to work on an Integration Services project, and want to have a table copied into another table, but this destination table needs a datetimestamp in the name, like tbl061708547pm.

I can get this to work, but if I try to use it with an into statement and a "+", I get an error telling me something is wrong around the "+":

DECLARE @Date char(23)
SET @Date = 'myTBL_' + CONVERT(char(23), GETDATE(), 14)
SELECT colA, @Date FROM myTBL

*****
Error with:
DECLARE @Date char(23)
SET @Date = 'myTBL_' + CONVERT(char(23), GETDATE(), 14)
SELECT * into @Date FROM myTBL

...and error with:
DECLARE @Date char(23)
SET @Date = CONVERT(char(23), GETDATE(), 14)
SELECT * into 'myTBL_ + @Date FROM myTBL;

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-17 : 19:51:54
You need to use dynamic SQL for that.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2008-06-17 : 21:57:17
As pointed out on the other forum, this is very likely to be a very, very wrong approach.
Whast are you trying to do this for?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-18 : 08:59:34
Instead of creating tables for each day, why dont you have a single table with datetime columns?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -