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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Table with date..? 1

Status
Not open for further replies.

Kujen

Technical User
Oct 13, 2007
57
SE
Hi all,

I would like to create a new table every day with todays date after the tablename.
like: MyDatabase..MyTable_yymmdd

How can I do this?

Kent J.
 
To create dynamic name you would need to use dynamic sql, e.g.
Code:
declare @sql nvarchar(8000), @d datetime
set @d = getdate()

set @sql = 'create table myFixedPart_' + CONVERT(VARCHAR,@d,112) + ' (myField1 ... '

print @sql

execute @sql

The question is - why do you need these tables created?
 
I want a daily copy, that I need to save, of a specific table. Something like:
============
declare @sql nvarchar(8000), @d datetime
set @d = getdate()
set @sql='MyTable' + CONVERT(VARCHAR,@d,112)

select * into @sql
from MyTable
===========

Is this possible?

Kent J.
 
No, you need to use dynamic SQL as I suggested already.
 
Markros... I think he may be saying that he wants to create the new table with the records from a source table rather then a new table from scratch.

Kujen needs to confirm that.

Simi
 
It is not important. Even the insert into or select into command would have to be dynamic.
 
OK!
So I the syntax should be:

declare @sql nvarchar(8000), @d datetimeset @d = getdate()
set @sql=
'select into MyTable' + CONVERT(VARCHAR,@d,112) + ' from MyTable'

print @sql
execute @sql
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top