| Author |
Topic |
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-03 : 18:42:24
|
| i want to sayselect all from "alldatabases".sys.indexesi dont want to use the sp_msforeachdb procedure because it doesnt work with what im trying to do further on, is there a way to do this and not using cursors either. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-01-03 : 18:47:30
|
| There is no way to do this without looping of some sort or using sp_msforeachdb.Tara Kizer |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-03 : 19:05:25
|
| okay can i use something likedeclare @i intselect @i=0while @i<3 db_name(@i).sys.indexes ...is there a way to do this? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-01-03 : 19:08:40
|
| There is no difference between a WHILE loop and a cursor. Either way, it'll require dynamic SQL.Tara Kizer |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-03 : 19:13:16
|
| ah right so performance gains of using a while over a cursor are minimal |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-03 : 19:13:51
|
| also for future reference is it possible at all to put a variable before .sys?such as@i.sys.indexes? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-01-03 : 19:14:46
|
| NoTara Kizer |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-03 : 19:16:22
|
| thnx |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-03 : 19:18:12
|
| so i take it this is the best way for me to do this:exec sp_msforeachdb'use ?INSERT INTO #minatest SELECTdb_name(database_id),phystat.page_count,i.fill_factor,OBJECT_NAME(i.object_id), i.name, phystat.avg_fragmentation_in_percent,newfragmentvalue = 0FROM sys.dm_db_index_physical_stats(NULL,NULL, NULL, NULL, NULL) phystatJOIN sys.indexes i ON i.object_id = phystat.object_id AND i.index_id = phystat.index_id WHERE phystat.avg_fragmentation_in_percent > 10 AND phystat.page_count < 10000' |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2007-01-03 : 20:33:33
|
select every user db sys.indexes: (note: will fail if any dbs are offline or if u dont have appropriate permissions. can exclude specific offline dbs in where clause of declare cursor)CREATE TABLE #t( db varchar(255), object_id int, name nvarchar(255), index_id int, type tinyint, type_desc nvarchar(60), is_unique bit, data_space_id int, ignore_dup_key bit, is_primary_key bit, is_unique_constraint bit, fill_factor tinyint, is_padded bit, is_disabled bit, is_hypothetical bit, allow_row_locks bit, allow_page_locks bit)Declare @db varchar(255)Declare c Cursorread_onlyfor select name from sys.databases where database_id > 4open cfetch next from c into @dbwhile (@@fetch_status = 0)Begin Exec ('INSERT #t SELECT ''' + @db + ''', * FROM ' + @db + '.sys.indexes' ) fetch next from c into @dbEndclose cdeallocate cselect * from #tdrop table #t |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-03 : 21:00:10
|
| the only problem with that is it doesnt show me the main field i need which is the index fragmentation percentage |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2007-01-03 : 21:21:19
|
| no, but it does do what u asked in original post. can use similar query for sys.dm_db_index_physical_stats though to get what u r after |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-03 : 21:33:44
|
| hmmm i dont think itll do what i want it to but thanx anyway |
 |
|
|
|