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 |
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-08 : 23:44:50
|
| im doign this Select @sql = Coalesce(@sql, '''') + '' INSERT INTO #fraglist EXEC (''''DBCC SHOWCONTIG (''+so.name+'') WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS'''') '' --USE UKbank select* FROM sysobjects so JOIN sysindexes si ON so.id = si.id WHERE so.type ='U' AND si.indid < 2 AND si.rows > 0and it bumping into a table with # in its name and returning this error:Incorrect syntax near '#'.is there a way around this |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-01-09 : 00:52:03
|
That's because you can't use EXEC inside a select statement. Use below statement to create DBCC SHOWCONTIG for each table and dump it to a temp table:Create Table #temp( seqno int identity(1,1), sqltext varchar(8000))Insert #tempSelect 'DBCC SHOWCONTIG ('+so.name+') WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS' from sysobjects soJOIN sysindexes siON so.id = si.idWHERE so.type ='U'AND si.indid < 2AND si.rows > 0And then loop on table records and execute each statement within the loop:While @i < @maxrowsbegin Select @sql = sqltext from #temp where seqno = @i INSERT INTO #fraglist Exec(@sql) set @i = @i + 1end Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-09 : 00:59:38
|
| no the exec works fine it does work the way im doing it... |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-09 : 01:00:00
|
| its just not treating special characters as normal char |
 |
|
|
rnbguy
Constraint Violating Yak Guru
293 Posts |
Posted - 2007-01-09 : 01:13:58
|
| if you have time see how ive done this code it works just gets stuck on one small thing i mensioned above:CREATE TABLE #fraglist (ObjectName CHAR (255),ObjectId INT,IndexName CHAR (255),IndexId INT,Lvl INT,CountPages INT,CountRows INT,MinRecSize INT,MaxRecSize INT,AvgRecSize INT,ForRecCount INT,Extents INT,ExtentSwitches INT,AvgFreeBytes INT,AvgPageDensity INT,ScanDensity DECIMAL,BestCount INT,ActualCount INT,LogicalFrag DECIMAL,ExtentFrag DECIMAL,)CREATE TABLE #fraglist1 (ObjectName CHAR (255),ObjectId INT,IndexName CHAR (255),IndexId INT,Lvl INT,CountPages INT,CountRows INT,MinRecSize INT,MaxRecSize INT,AvgRecSize INT,ForRecCount INT,Extents INT,ExtentSwitches INT,AvgFreeBytes INT,AvgPageDensity INT,ScanDensity DECIMAL,BestCount INT,ActualCount INT,LogicalFrag DECIMAL,ExtentFrag DECIMAL,)CREATE TABLE #statstable (DatabaseName VARCHAR(40),ObjectName CHAR (255),ObjectId INT,IndexName CHAR (255),IndexId INT,ScanDensity DECIMAL,NewScanDensity DECIMAL,LogicalFrag DECIMAL,NewLogicalFrag DECIMAL,CountPages INT)exec sp_msforeachdb'DECLARE @sql nvarchar(4000)if (''?'') <> ''tempdb'' and (''?'') <> ''msdb'' and (''?'') <> ''model'' and (''?'') <> ''master'' BEGIN USE ? Select @sql = Coalesce(@sql, '''') + '' INSERT INTO #fraglist EXEC (''''DBCC SHOWCONTIG (''+so.name+'') WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS'''') '' --USE UKbank select* FROM sysobjects so JOIN sysindexes si ON so.id = si.id WHERE so.type =''U'' AND si.indid < 2 AND si.rows > 0 exec sp_executesql @sql insert into #statstable select DatabaseName=''?'',ObjectName,ObjectId,IndexName,IndexID,ScanDensity,0,LogicalFrag,0,CountPages from #fraglist where scandensity < 90 --and CountPages > 1000 set @sql ='''' Select @sql = Coalesce(@sql, '''') + ''DBCC DBREINDEX(''+RTRIM(#fraglist.ObjectName)+'')WITH NO_INFOMSGS;'' from #fraglist,sysobjects where scandensity < 90 and sysobjects.name = #fraglist.ObjectName and CountPages > 1000 exec sp_executesql @sql set @sql='''' Select @sql = Coalesce(@sql, '''') + '' INSERT INTO #fraglist1 EXEC (''''DBCC SHOWCONTIG (''+so.name+'') WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS'''') '' FROM sysobjects so JOIN sysindexes si ON so.id = si.id WHERE so.type =''U'' AND si.indid < 2 AND si.rows > 0 exec sp_executesql @sqlEND'update #statstable set NewScanDensity = (select TOP 1 fl1.ScanDensity from #fraglist1 fl1 where fl1.ObjectId = #statstable.ObjectId and fl1.IndexId = #statstable.IndexId) update #statstable set NewLogicalFrag = (select TOP 1 fl1.LogicalFrag from #fraglist1 fl1 where fl1.ObjectId = #statstable.ObjectId and fl1.IndexId = #statstable.IndexId)select * from #statstabledrop table #fraglistdrop table #fraglist1drop table #statstable |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-01-09 : 04:10:18
|
I expect your #TempTable is out of scope in the EXECInstead ofSelect @sql = Coalesce(@sql, '''') + '' INSERT INTO #fraglist EXEC (''''DBCC SHOWCONTIG (''+so.name+'') WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS'''')''either use a permanent table, or you may need to use a CursorThis is what we do: (change the name of the Temp table etc. as appropriate for your code; you may also have to change the system tables etc. if you are using SQL 2005):-- Declare cursorDECLARE CUR_TABLES CURSOR FOR SELECT so.id, so.[name], si.[name] FROM sysobjects so JOIN sysindexes si ON so.id = si.id WHERE so.type ='U' AND si.indid < 2 AND si.rows > 0-- Open the cursorOPEN CUR_TABLES-- Loop through all the tables in the databaseFETCH NEXTFROM CUR_TABLESINTO @tableid, @so_Name, @si_NameWHILE @@FETCH_STATUS = 0BEGIN -- Do the showcontig of all indexes of the table SELECT @tableidchar = CONVERT(varchar(20), @tableid), @strSQL = 'DBCC SHOWCONTIG (' + @tableidchar + ') WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS' INSERT INTO #FRAGLIST EXEC (@strSQL) FETCH NEXT FROM CUR_TABLES INTO @tableid, @so_Name, @si_NameEND-- Close and deallocate the cursorCLOSE CUR_TABLESDEALLOCATE CUR_TABLESKristen |
 |
|
|
|
|
|
|
|