September 24, 2008 at 12:28 am
Please can someone help.
I am trying to write SQL that will create a table name with a date stamp (mmyy) derived from getdate().
Any suggestions?
Ta
September 24, 2008 at 1:15 am
Hello,
You could use dynamic SQL to build the Create Table statement, including the DatePart function to extract the mmyy of GetDate().
Regards,
John Marsh
www.sql.lu
SQL Server Luxembourg User Group
September 24, 2008 at 1:36 am
Roger (9/24/2008)
Please can someone help.I am trying to write SQL that will create a table name with a date stamp (mmyy) derived from getdate().
Any suggestions?
Ta
Are you trying to create tables every month?
declare @sql varchar(1000)
set @sql='create table test_'+right('0'+cast(month(getdate()) as varchar(2)),2)+right(year(getdate()),2)
exec(@sql)
Failing to plan is Planning to fail
September 24, 2008 at 1:57 am
Madhivanan
Many thanks for the quick response.
When I run your code I get an error message "incorrect syntax near test_0908"
I will need to run this code each month as was hoping to adapt it 'select * into' as follows:
declare @sql varchar(1000)
set @sql=
'select * into dbo.test_'+right('0'+cast(month(getdate()) as varchar(2)),2)+right(year(getdate()),2) from mra_arch_tbl
exec(@sql)
this generates an error message "incorrect syntax near the keyword from' .
Can you help?
September 24, 2008 at 2:15 am
Hello.
With dynamic SQL you have to build a valid Create table or Select statement in a variable (in this example @sql).
The easiest way to see if it is valid is to use Print @sql and then copy the statement generated into another query window and execute it.
You need to be careful with the quotes when building dynamic SQL, and this is what is wrong with your select. (Please see below)
Regards,
John Marsh
declare @sql varchar(1000)
set @sql=
'select * into dbo.test_'+right('0'+cast(month(getdate()) as varchar(2)),2)+right(year(getdate()),2) + ' from mra_arch_tbl'
Print @sql
-- exec(@sql)
www.sql.lu
SQL Server Luxembourg User Group
September 24, 2008 at 2:32 am
John
Thanks for the advice.
The dynamic sql seems to be working OK as it is generating 0908 the problem I have is how to incorporate the dynamic sql into my query.
I thought my sql code would need speech marks around it like:
declare @sql varchar(1000)
set @sql=
'select * into dbo.test_'+right('0'+cast(month(getdate()) as varchar(2)),2)+right(year(getdate()),2) 'from mra_arch_tbl'
exec(@sql)
but this is obviously wrong
Then I tried
declare @sql varchar(1000)
set @sql=right('0'+cast(month(getdate()) as varchar(2)),2)+right(year(getdate()),2)
select * into dbo.test_+exec(@sql) from mra_arch_tbl
but again it doesn't work.
Please can you advise.
Ta
September 24, 2008 at 2:37 am
Hello Again,
The first version is more valid as you need to create a complete SQL statement. You can not create partially dynamic SQL as in your second version.
Below is a corrected version.
Regards,
John Marsh
declare @sql varchar(1000)
set @sql=
'select * into dbo.test_'+right('0'+cast(month(getdate()) as varchar(2)),2)+right(year(getdate()),2) + ' from mra_arch_tbl'
Print @sql -- Display for debug puposes
exec(@sql)
www.sql.lu
SQL Server Luxembourg User Group
September 24, 2008 at 2:41 am
John
Excellent code many thanks for your help.
September 27, 2008 at 9:20 pm
I'm not sure who gave you the requirements on these file names, but the yyyymm format will serve you much better and for much longer.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply