SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to get alter index statement from select statement


How to get alter index statement from select statement

Author
Message
Akayisi
Akayisi
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 198
Hi guys i want to get alter index statement from my select

In order to do thAT I have this,

select 'ALTER INDEX '+name+ ' ON'+' rebuild' from sys.indexes
where object_id = (select object_id from sys.objects where name = 'TABLE_NAME')
and
name is not null

the output is

ALTER INDEX index_name ON rebuild

IT SHOULD BE;


ALTER INDEX index_name ON TABLE_NAME rebuild

I'm missing the table name part. I tried to use INFORMATION_SCHEMA.columns.TABLE_NAME but it did not work since it produces 3 index statements (because i have three tables). Any suggestions ?

Thanks in advance ....
Adi Cohn
Adi Cohn
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3645 Visits: 6500
Here is one way of doing it:


select 'alter index [' + si.name + '] on [' + schema_name(so.schema_id) + '].[' + so.name + '] rebuild with (online = on)'
from sys.indexes si inner join sys.objects so on si.object_id = so.object_id
where si.object_id = object_id('TableName')



Adi

--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Akayisi
Akayisi
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 198
Thanks a lot by the way i guess this is also working

select 'ALTER INDEX '+sys.indexes.name+ ' ON '+sys.tables.name+' rebuild' from sys.indexes,sys.tables
where sys.indexes.object_id=sys.tables.object_id
and
sys.indexes.name is not null
Adi Cohn
Adi Cohn
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3645 Visits: 6500
Akayisi (1/7/2014)
Thanks a lot by the way i guess this is also working

select 'ALTER INDEX '+sys.indexes.name+ ' ON '+sys.tables.name+' rebuild' from sys.indexes,sys.tables
where sys.indexes.object_id=sys.tables.object_id
and
sys.indexes.name is not null


This can work as long as you don't have more then 1 schema. Also this will create a script for all indexes on all the tables and not just one table as you asked in the original post.

Adi

--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search