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.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Automating a script

Author  Topic 

David.
Starting Member

2 Posts

Posted - 2010-11-24 : 16:23:05
Hi

I picked up this usefull sript for deleting en mass

select 'delete from '+name from sysobjects where type='u'

this returns a table of

delete from foo
delete from baa
delete from etc

does anybody know if I can extend this to allow more text to occur after the [name from sysobjects where type='u'] for example

select
'alter table' + name from sysobjects where type='u' +
'Add Index idx_' + name from sysobjects where type='u' +
'(' + name from sysobjects where type='u' + ')'

The above doesn't work, greatfull for any ideas

Regards

David

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-24 : 16:48:09
You overcomplicated it. Here you go, although it's not going to work as you intend it to since you are missing the column to be indexed.

select
'alter table ' + name + ' add index idx_' + name + '(' + name + ')'
from sysobjects where type='u'

And you should be using the INFORMATION_SCHEMA views instead of sysobjects. Try INFORMATION_SCHEMA.COLUMNS and INFORMATION_SCHEMA.TABLES.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

David.
Starting Member

2 Posts

Posted - 2010-11-25 : 06:07:29
Great, thank you.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-11-25 : 14:07:53
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -