Problem : DBCC UPDATEUSAGE for multiple databases

Problem : DBCC UPDATEUSAGE for multiple databases

Hello,

We have problem with our SQL databases. We got advice from our software developer company to run this command (look below). The problem is that this same problem occur with hundreds databases. So with this command I need to run it hundreds of times. How I can run this same command for all databases in one time? Or is it possible for all databases which starts TK*? Most of databases are TK* and all them have this same problem. There is also couple other kind of databases and with them is no problems.

USE TK0010;
GO
DBCC UPDATEUSAGE (TK0010) WITH NO_INFOMSGS;
GO

Then other question. These all databases are transfered from SQL2000 to SQL2005. At the moment these databases are in SQL2000 mode. There is too much databases to change them mode manually. How I could script this for all databases?


Solution  : DBCC UPDATEUSAGE for multiple databases

It’s worth notting that DBCC UPDATEUSAGE is always used on a single database at a time but there’s a work around in case you want to run through all your user databases.

Use the code i have attached as you wish.

— To update stats on the current DB
DBCC UPDATEUSAGE(0);

— To go to a different database
DBCC UPDATEUSAGE (‘db name’);

— To browse all databases (using a simple cursor)
— Cursor updates usage for all non system databases
declare @name sysname, @str varchar(500)
declare cur_dbs cursor
for select [name] from master.sys.databases
where [name] not in (‘master’, ‘tempdb’, ‘model’, ‘msdb’) — Ignore system databases
open cur_dbs
fetch next from cur_dbs into @name
while @@fetch_status = 0
begin
select @str = ‘DBCC UPDATEUSAGE(”’[email protected]+”’);’
execute(@str)
fetch next from cur_dbs into @name
end
close cur_dbs
deallocate cur_dbs