Click here to Skip to main content
15,885,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to create views,indexes and stroed procedure name with date.

Ex:-At present i can create index (Create Index MY_INDEX ON dbo.TableName) so index name will be
"MY_INDEX"
But i want it as "MY_INDEX_20131119"?
So i want something which can do it like

CREATE INDEX MY_INDEX+GETDATE ON dbo.TableName
( )

Finally i want to create my script name with concatenation of execution date.

Thanks
Posted
Updated 18-Nov-13 19:37pm
v2

1 solution

You have to use dynamic scripts - as SQL does not allow variable in CREATE TABLE...

A T-SQL sample...
SQL
EXECUTE('CREATE TABLE MYINDEX_' + REPLACE(CAST(GETDATE() AS NVARCHAR), ' ', '')+ ' (ID INT NOT NULL, NAME NVARCHAR(120))')
 
Share this answer
 
v2
Comments
StackQ 19-Nov-13 2:36am    
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'CREATE TABLE MYINDEX_'.
Kornfeld Eliyahu Peter 19-Nov-13 3:00am    
CAST(GETDATE() AS NVARCHAR) may give you a time with : inside it...
Try to format the date part in some other way (: is forbidden in table names)...
StackQ 19-Nov-13 3:45am    
1st thing it's always giving error at 'CREATE TABLE MYINDEX_', after solving it i can go ahead.
Kornfeld Eliyahu Peter 19-Nov-13 3:47am    
Replace EXEC with PRINT and copy here the result please...
StackQ 19-Nov-13 4:05am    
O/P is:- CREATE TABLE MYINDEX_Nov1920132:33PM (ID INT NOT NULL, NAME NVARCHAR(120))


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900