| Author |
Topic |
|
surefooted
Posting Yak Master
188 Posts |
Posted - 2004-10-06 : 11:03:19
|
| Ok, I was wondering if there was a way to reindex a database's indexes table by table and indiviually in a script without using a cursor or a psuedo-cursor(while loop). Has anyone written something like this before? I have ahad suggetions here of generating an output file then executing it, but I would like to keep it all t-sql. Thanks in advance.-JonNow a "Yak Posting Veteran". |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-10-06 : 12:49:51
|
For some reason, this stops at 4000DECLARE @SQL varchar(8000)SELECT @SQL = ''SELECT @SQL = @SQL + 'DBCC DBREINDEX ('''+TABLE_NAME+''''+') 'FROM INFORMATION_SCHEMA.TablesSELECT LEN(@SQL), @SQLEXEC(@SQL)Brett8-) |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-10-06 : 13:33:08
|
| why do you need to avoid a loop or a cursor in this case? I cannot think of any reason why that would not be acceptable.- Jeff |
 |
|
|
surefooted
Posting Yak Master
188 Posts |
Posted - 2004-10-07 : 08:14:50
|
| Brett, I was going to do that, but what happens when your out grow your variable? Jeff, because of a way of thinking here. I have written it loop based.-JonNow a "Yak Posting Veteran". |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2004-10-07 : 08:50:12
|
| There is YET ANOTHER undocumented command, DBCC DBREINDEXALL. I DO NOT recommend it though, as I've never gotten satisfactory performance from it (for all I know, it hangs and never comes back). The syntax is:DBCC DBREINDEXALL('myDatabase')DO NOT use this on a production server until you have THOROUGHLY tested it on a separate server that's not heavily used. I'll still recommend that you use regular DBREINDEX though, it has been much much faster for me.BTW, generating the DBREINDEX script to a file and running it is perfectly OK, you can use the osql utility to execute the file. You can call it from T-SQL using xp_cmdshell. |
 |
|
|
surefooted
Posting Yak Master
188 Posts |
Posted - 2004-10-07 : 08:58:23
|
| Cool thanks Rob.-JonNow a "Yak Posting Veteran". |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-10-07 : 09:10:57
|
i bet $5 he's going ti use it Go with the flow & have fun! Else fight the flow |
 |
|
|
surefooted
Posting Yak Master
188 Posts |
Posted - 2004-10-07 : 10:19:35
|
Nah I just wrote a batch file that uses the osql method he mentioned. I took the more complex challenge. If anyone cares I can post it.-JonNow a "Yak Posting Veteran". |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-10-07 : 10:24:18
|
post it.Go with the flow & have fun! Else fight the flow |
 |
|
|
surefooted
Posting Yak Master
188 Posts |
Posted - 2004-10-07 : 10:43:04
|
| [code]@echo offset server=%1set db=%2set uid=-U%3set pwd=-P%4if "%1"=="" goto ErrorHandlerif "%2"=="" set db=masterif "%3"=="" set uid=-Eif "%4"=="" set pwd=echo Checking for existence of reindex view on server %server% database %db%...osql -S %server% %uid% %pwd% -d %db% -l 1 -Q"if object_id('dbo.vw_DBCC_TABLE_REINDEX') is not null drop view dbo.vw_DBCC_TABLE_REINDEX"if errorlevel 1 goto LoginFailureecho Creating a view with dbcc reindex commands on server %server% database %db%...osql -S %server% %uid% %pwd% -d %db% -Q"create view dbo.vw_DBCC_TABLE_REINDEX as select cmd = 'DBCC DBREINDEX (''' + db_name() + '.dbo.[' + so.name + ']'',' + si.name + ', 0)' from sysindexes si inner join sysobjects so on si.id= so.id where objectproperty(so.id, 'IsMSShipped') = 0 and objectproperty(so.id, 'IsTable') = 1 and si.indid > 0 and si.indid < 255 and (si.status & 64)=0"if "%uid%"=="-E" set uid=-Techo Generating the final script for server %server% database %db%...bcp %db%.dbo.vw_DBCC_TABLE_REINDEX out DBCC_TABLE_REINDEX.SQL -S %server% %uid% %pwd% -cif "%uid%"=="-T" set uid=-Eecho Processing DBCC_TABLE_REINDEX script on %server% database %db%...osql -S %server% %uid% %pwd% -d %db% -i DBCC_TABLE_REINDEX.SQL -h-1 -n -w 256 -o %db%_DBCC_TABLE_REINDEX.LOGecho Check %db%_DBCC_TABLE_REINDEX.LOG for any errors!goto end:ErrorHandlerecho No server and/or database specified!echo Execution returned Error Code %ERRORLEVEL%goto end:LoginFailureecho Failed to login to %server%!echo Execution returned Error Code %ERRORLEVEL%:end@echo on[/code]Using previous batch files as a guide.-JonNow a "Yak Posting Veteran". |
 |
|
|
|