Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically created a table name in a make-a-table query 1

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
0
0
US
I would like to make part of the name of a make a table query be the current date.

Something like so...

SELECT * INTO MYTABLE + GETDATE()
FROM MASTERFILE

I've tried a couple of combinations and have failed.

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
declare @sql varchar(255)

set @sql = 'SELECT * INTO MYTABLE' + convert(char(4),DATEPART ( yyyy , GETDATE() )) +
RIGHT('00'+convert(varchar(4),DATEPART ( mm , GETDATE() )),2) +
RIGHT('00'+convert(varchar(4),DATEPART ( dd , GETDATE() )),2) +
' FROM MASTERFILE'

exec @sql
 
just to simplify snag's solution:

declare @sql varchar(255)

set @sql = 'SELECT * INTO MYTABLE' + convert(char(8),getdate(),112) + ' FROM MASTERFILE'

exec @sql

~Brian
 
That's cool guys, but it is telling me that it cannot find the stored procedure 'SELECT * INTO MYTABLE20040405 FROM MASTERFILE'

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
Try This ...

declare @sql_command VarChar(200)

set @sql_command =

'SELECT * INTO MYTABLE' +
CONVERT(Char,GETDATE(),101) + ' FROM MASTERFILE'

exec (@sql_command)

Thanks

J. Kusch
 
Thanks guys. Jay the parenthesis did it!

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top