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.
| Author |
Topic |
|
Sqlraider
Yak Posting Veteran
65 Posts |
Posted - 2010-11-09 : 14:51:37
|
| I'm new to SQL Server.I need to create an Index for 40+ tables that are Imported to 2008 server from 2000. Each Table has the same FieldName I wish to create the Index on. I would like to create a script that will pull in the TableName & use it as part of the Index name. What I have only works for one table & I'm not sure how to write/pass/loop for multiple tables. Here is what I have:USE IMPORT_DATABASEGOIF EXISTS (SELECT name FROM sys.indexes WHERE name = N'NDX_TableName_DOC') DROP INDEX NDX_TableName_DOC ON TableName ;GOCREATE NONCLUSTERED INDEX NDX_TableName_DOC ON TableName (DateOfChange DESC);GOI want to pass the TableName for each of the Tables I have with the FieldName of DateOfChange without re-typing these statements 40+ times. The end result is that I have an Index on each of these tables so that I can query on what has been changed in the last date range (like in the last week).Thanks. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Sqlraider
Yak Posting Veteran
65 Posts |
Posted - 2010-11-09 : 17:05:35
|
| OK, I've tried it but I keep getting errors. I'm overlooking something.Here is what I tried & errors I'm getting:CREATE NONCLUSTERED INDEX "select 'ndx_' + TABLE_NAME + '_DOC' from INFORMATION_SCHEMA.COLUMNSwhere COLUMN_NAME='DateOfChange'"ON TABLE_NAME(DateOfChange DESC)Error - "right after ON" Invalid object name TABLE_NAME & Column name 'DateOfChange' does not exist in the target table or view. Also tried:CREATE NONCLUSTERED INDEX "select 'ndx_' + TABLE_NAME + '_DOC ON ' + TABLE_NAME + '(DateOfChange DESC)' from INFORMATION_SCHEMA.COLUMNSwhere COLUMN_NAME='DateOfChange'"Error -Msg 103, Level 15, State 4, Line 11The identifier that starts with 'select 'ndx_' + TABLE_NAME + '_DOC ON ' + TABLE_NAME + '(DateOfChange DESC)' from INFORMATION_SCHEMA.COLUMNSwhere COLUMN_NAM' is too long. Maximum length is 128.Msg 102, Level 15, State 1, Line 11Incorrect syntax near 'select 'ndx_' + TABLE_NAME + '_DOC ON ' + TABLE_NAME + '(DateOfChange DESC)' from INFORMATION_SCHEMA.COLUMNSwhere COLUMN_NAM'.Thanks. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Sqlraider
Yak Posting Veteran
65 Posts |
Posted - 2010-11-09 : 17:20:09
|
| I did run you're query as written & just got result message(s) such as for each table:create nonclustered index ndx_Assessments_DOC ON Assessments(DateOfChange DESC)But it did NOT create the Index.Basically it pulled the table name in but did not execute the 'create index' statement. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Sqlraider
Yak Posting Veteran
65 Posts |
Posted - 2010-11-09 : 18:39:59
|
| Sorry I misunderstood your answer.It does exactly as I asked, Thank You very much. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|